C program to find average of three numbers
C program to find average of three numbers
Problem Description
This program finds the average of three numbers provided by the user. In this program, the variables used are of type float instead of integer because average can be in fraction.
C program to find average of three numbers - Source code
#include<stdio.h> int main() { float a,b,c; float average=0; printf("Enter three numbers to find their average: \n"); scanf("%f%f%f",&a,&b,&c); average =(a+b+c)/3.0; // formula to calculate average // of three numbers. printf("\n Average of three numbers is \t %f",average); return 0; }
Program Output
Case 1: Enter three numbers to find their average: 56 34 78 Average of three numbers is 56.000000 Case 2: Enter three numbers to find their average: 568 673 237 Average of three numbers is 492.666656
Program Explanation
1. This program first prompt the user to input three numbers whose average is required. 2. The user can even enter the decimal numbers/ fractional numbers as the data type of numbers is float. 3. The average is calculated using the mathematical formula (a+b+c)/3.0 and printed on the screen.