C Program to find the average of two numbers
|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-
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
- 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. - 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. - 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
- Initially, the program will prompt the user to enter the two numbers and store the values in variables say (number1 and number2).
- 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. - 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 usingreturn (float)(x + y)/2
. - 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!! ?
Does not consider integer overflow