How to find Average of Two Numbers in C - Step by Step Examples
How to find Average of Two Numbers in C ?
Answer
To find the average of two numbers in C, you can add the two numbers and then divide the sum by 2.
✐ Examples
1 Average of Two Numbers
In this example,
- We declare two variables
a
andb
to store the numbers for which we want to find the average. - We initialize these variables with the desired values.
- We add the two numbers
a
andb
and store the sum in a variablesum
. - We then divide the
sum
by 2 to calculate the average and store it in a variableaverage
. - Finally, we print the value of
average
to display the average of the two numbers.
C Program
#include <stdio.h>
int main() {
int a = 5, b = 10;
int sum = a + b;
float average = sum / 2.0;
printf("Average of %d and %d is %.2f\n", a, b, average);
return 0;
}
Output
Average of 5 and 10 is 7.50
Summary
In this tutorial, we learned How to find Average of Two Numbers in C language with well detailed examples.