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
}
This line includes the standard input and output library in C. It allows us to use functions like printf() and scanf().
The main() function is the starting point of every C program. The execution of the program begins from here.
Here, four variables are declared:
a → stores the first numberb → stores the second numberc → stores the third numberd → stores the average resultThe float data type is used because the average may contain decimal values.
The printf() function is used to display messages on the screen.
printf("Enter first number: ");
This asks the user to enter the first number.
The scanf() function is used to take input from the user.
%f is used for float values&a gives the memory address of variable ad=(a+b+c)/3;
This line adds the three numbers and divides the sum by 3 to calculate the average.
printf("%f",d);
This prints the final average value on the screen.
Enter first number: 10
Enter second number: 20
Enter third number: 30
20.000000
This program is a simple example of using:
It helps beginners understand how basic calculations are performed in C programming.