Average of Two Numbers in C Language

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
ADVERTISEMENT

Conclusion

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