C program to calculate the average of two numbers has been shown here. Average of two numbers is described by the sum of those numbers divided by 2. Suppose $a$ and $b$ are two numbers, then the average is calculated as $\frac{a+b}{2}$. For example if the two numbers are 5 and 6, the average is (5 + 6)/2 = 5.5.
1. Algorithm to Calculate the Average of Two Numbers
1. Take two numbers a and b as inputs
2. Compute x = (a + b)/2
3. Declare x as the average and exit program
2. Pseudocode to Calculate the Average of Two Numbers
Input: Two numbers $a$ and $b$
Output: Average of $a$ and $b$
1. Procedure average($a$, $b$):
2.
3.
4. End Procedure
3. Time Complexity to Calculate the Average of Two Numbers
Time Complexity: O(1)
4. C Program to Calculate the Average of Two Numbers
/************************** alphabetacoder.com C Program to calculate the average of two numbers ***************************/ #include <stdio.h> int main() { // declare variables float a, b, x; //take input of the numbers printf("Enter the first number = "); scanf("%f", & a); printf("Enter the second number = "); scanf("%f", & b); // calculate average x = (a + b) / 2; // display the result printf("Average of %f and %f is %f", a, b, x); return 0; }
Output
Enter the first number = 12.5
Enter the second number = 10
Average of 12.500000 and 10.000000 is 11.250000