Program to find average of two numbers in C++

#include <stdio.h> int main() { int n, i; float num[100], sum = 0.0, avg; printf("Enter the numbers of elements: "); scanf("%d", &n); while (n > 100 || n < 1) { printf("Error! number should in range of (1 to 100).\n"); printf("Enter the number again: "); scanf("%d", &n); } for (i = 0; i < n; ++i) { printf("%d. Enter number: ", i + 1); scanf("%f", &num[i]); sum += num[i]; } avg = sum / n; printf("Average = %.2f", avg); return 0; }

Output

Enter the numbers of elements: 6 1. Enter number: 45.3 2. Enter number: 67.5 3. Enter number: -45.6 4. Enter number: 20.34 5. Enter number: 33 6. Enter number: 45.6 Average = 27.69

Here, the user is first asked to enter the number of elements. This number is assigned to n.

If the user entered integer is greater less than 1 or greater than 100, the user is asked to enter the number again. This is done using a while loop.

Then, we have iterated a for loop from i = 0 to i . In each iteration of the loop, the user is asked to enter numbers to calculate the average. These numbers are stored in the num[] array.

scanf("%f", &num[i]);

And, the sum of each entered element is computed.

sum += num[i];

Once the for loop is completed, the average is calculated and printed on the screen.

Arushi Dwivedi | April 4, 2021 | C Programming Example |

In this C Programming example, we will implement the program to find the average of two numbers and print the output on the console.

1. How to find the Average of two numbers?

The average of two numbers can be calculated using two methods.

  • The first method is the standard method where we prompt the user to input two integer type numbers, store them in the variables (say number 1 and number 2), calculate its average, and print the output on the console.
  • The second method is using function where we prompt the user to input two integer type numbers, store them in the variables (say number 1 and number 2), call the function, calculate its average and print the output on the console.
Example Input: first Number: 12 second Number: 13 Output: The Product is: 12.50

Helpful topics to understand this program better are-

  • Data Types in C
  • Console Input / Output in C
  • Functions in C

2. C Program to find the average of two numbers

Let’s discuss the execution(kind of pseudocode) for the program to find the average of two numbers in C.

2.1. Standard Method

  1. Initially, the program will prompt the user to enter number1 and number2 and store the value in these variables and float average declares that the average of two numbers will be of float datatype.
  2. After the values are stored the average is calculated using average= (float)(number1 + number2)/2 , here the result of the two numbers is converted into float datatype and then stored on average.
  3. After the calculation has been done the output gets printed on the console.

Let us now implement the above execution of the program to find the average of two numbers in C.

#include <stdio.h> int main(){ int number1, number2; float average; printf("Enter the first number: "); scanf("%d",&number1); printf("Enter the second number: "); scanf("%d",&number2); average= (float)(number1 + number2)/2; printf("The Average of %d and %d is: %.2f",number1,number2,average); return 0; }

Note: In the above program, %.2f is used to display floating point number up to two decimal places.

Output Enter the first number: 1 Enter the second number: 3 The Average of 1 and 3 is: 2.00

2.2. Using Function

  1. Initially, the program will prompt the user to enter the two numbers and store the values in variables say (number1 and number2).
  2. Then the numbers are passed in avg = average(number1, number2) where the user-defined average function is called and inputs are then pushed into the function.
  3. In float average(int x, int y) , int x, int y represents number1 and number2. The inputs are passed as arguments in the function and then the average of those numbers are calculated using return (float)(x + y)/2.
  4. After the output is calculated it is then returned and printed on the console.
#include <stdio.h> float average(int x, int y){ return (float)(x + y)/2; } int main(){ int number1, number2; float avg; printf("Enter the first number: "); scanf("%d",&number1); printf("Enter the second number: "); scanf("%d",&number2); avg = average(number1, number2); printf("The Average of %d and %d is: %.2f",number1,number2,avg); return 0; }

Note: In the above program, %.2f is used to display floating point number up to two decimal places.

Output Enter the first number: 5 Enter the second number: 4 The Average of 5 and 4 is: 4.50

3. Conclusion

In this C Programming example, we have discussed how to find the average of two numbers in C.

Helpful Links

Please follow C Programming tutorials or the menu in the sidebar for the complete tutorial series.

Also for the example C programs please refer to C Programming Examples.

All examples are hosted on Github.

Recommended Books

An investment in knowledge always pays the best interest. I hope you like the tutorial. Do come back for more because learning paves way for a better understanding

Do not forget to share and Subscribe.

Happy coding!! 😊

Recommended -

C Program to find the average of two numbers was last modified: April 4th, 2021 by Arushi Dwivedi

Would love your thoughts, please comment.x

To find the average of two numbers in C programming, find their sum using C Addition Operator and divide the sum with 2 (since there are only two numbers) using C Division Operator.

C Program

In the following program, we read two numbers into n1 and n2 from user, and find their average using the formula (n1 + n2) / 2.

main.c

#include <stdio.h> int main() { int n1, n2; float avg = 0; printf("Enter n1 : "); scanf("%d", &n1); printf("Enter n2 : "); scanf("%d", &n2); avg = (float)(n1 + n2) / 2; printf("Average : %f\n", avg); return 0; }

Output

Enter n1 : 2 Enter n2 : 3 Average : 2.500000 Program ended with exit code: 0

Output

Enter n1 : 10 Enter n2 : 20 Average : 15.000000 Program ended with exit code: 0

Conclusion

In this C Tutorial, we learned how to find the average of two numbers using C program.

Here we will write two C programs to find the average of two numbers(entered by user).

Example 1: Program to find the average of two numbers

#include <stdio.h> int main() { int num1, num2; float avg; printf("Enter first number: "); scanf("%d",&num1); printf("Enter second number: "); scanf("%d",&num2); avg= (float)(num1+num2)/2; //%.2f is used for displaying output upto two decimal places printf("Average of %d and %d is: %.2f",num1,num2,avg); return 0; }

Output:

Enter first number: 12 Enter second number: 13 Average of 12 and 13 is: 12.50

Example 2: Program to find the average using function

In this program, we have created a user defined function average() for the calculation of average. The numbers entered by user are passed to this function during function call.

#include <stdio.h> float average(int a, int b){ return (float)(a+b)/2; } int main() { int num1, num2; float avg; printf("Enter first number: "); scanf("%d",&num1); printf("Enter second number: "); scanf("%d",&num2); avg = average(num1, num2); //%.2f is used for displaying output upto two decimal places printf("Average of %d and %d is: %.2f",num1,num2,avg); return 0; }

Output:

Enter first number: 20 Enter second number: 13 Average of 20 and 13 is: 16.50

Check out the related C Programs:

Neuester Beitrag

Stichworte