### Input two numbers and work out their sum, average and sum of the squares of the numbers.

**Source Code**

Brief explanation is provided after the source code.

#include <stdio.h> int main(int argc, char ** argv) { float number1, number2; float sum; printf("Input two numbers:\n"); scanf("%f%f", &number1, &number2); sum = number1 + number2; printf("Sum: %.2f\n", sum); printf("Average: %.2f\n", sum/2); printf("Sum of squares: %.2f\n", number1*number1 + number2*number2); return 0; }

When you compile and execute the above program it produces the following result on Linux:

Input two numbers: 5 6 Sum: 11.00 Average: 5.50 Sum of squares: 61.00

**Brief Explanation**

- The program requests the user to enter two numbers.
- scanf function read the numbers and assign them to variables number1 and number2.
- Using the arithmetic operator + (for addition), the two numbers are added and the result assign to the variable sum.
- The average is computed by dividing (/) the sum by 2.
- The arithmetic operator * (for multiplication) is used to compute the square of each number.
- printf function is used to display the results to the stdout.

## Comments

RSS feed for comments to this post