C Program to Find the Average of 3 Numbers

In this program, we will learn how to calculate the average of three numbers using the C programming language.

The user enters three numbers, and the program adds them together and divides the result by 3 to find the average.

#include<stdio.h>
main(){
    float a,b,c,d; //define the variable as float type
    printf("Enter first number: ");
    scanf("%f",&a); //Enter the first variabele
    printf("Enter secound number: ");
    scanf("%f",&b); //Enter the second variable
    printf("Enter third number: ");
    scanf("%f",&c); //Enter the third variable
    d=(a+b+c)/3; //Formula for average
    printf("%f",d); //Print the average
}

Explanation of the Program

#include

This line includes the standard input and output library in C. It allows us to use functions like printf() and scanf().

main()

The main() function is the starting point of every C program. The execution of the program begins from here.

float a,b,c,d;

Here, four variables are declared:

  • a → stores the first number
  • b → stores the second number
  • c → stores the third number
  • d → stores the average result

The float data type is used because the average may contain decimal values.

printf()

The printf() function is used to display messages on the screen.

printf("Enter first number: ");

This asks the user to enter the first number.

scanf()

The scanf() function is used to take input from the user.

  • %f is used for float values
  • &a gives the memory address of variable a

Formula for Average

d=(a+b+c)/3;

This line adds the three numbers and divides the sum by 3 to calculate the average.

Displaying the Output

printf("%f",d);

This prints the final average value on the screen.

Example Output

Enter first number: 10
Enter second number: 20
Enter third number: 30
20.000000

Conclusion

This program is a simple example of using:

  • Variables
  • User input
  • Arithmetic operations
  • Output functions

It helps beginners understand how basic calculations are performed in C programming.