In this tutorial you will learn about the C Program for Average of Two Numbers and its application with practical example.
C Program to Average of Two Numbers
In this tutorial, we will learn to create a C program that will Calculate the Average of Two Numbers in C programming.
Prerequisites
Before starting with this tutorial, we assume that you are the best aware of the following C programming topics:
- Operators in C Programming.
- Basic Input and Output function in C Programming.
- Basic C programming.
- Arithmetic operator in C programming.
What is the average of number?
The average of numbers in mathematics means that all the numbers are added and then the sum of all the numbers is divided by the total number of objects.
It means if there are 4 number as follows {2,4,6,8} then the average will be:-
average = 2+4+6+8/4
average = 5.
Algorithm:-
1 2 3 4 5 6 7 8 9 |
1. Declaring the variables for the program. 2. Taking the input number from the user to find the average. 3. Calculating the average of the those numbers. 4. Printing the result average of that number to the user. 5. End the program. |
Program to Calculate the average of a Number
In this program, we will take two numbers in input from the user. Then we will add that number and store it in a variable. After that, we will divide the sum of those numbers by 2 to find the average. At last, we will print the average of two numbers.
With the help of this program, we can find the average of two numbers.
Program code:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
/* C program to find average of two numbers. */ #include <stdio.h> int main() { //Declaring the required variable for the program. int no1, no2; float average; //Taking the input number 1 from the user. printf("Enter the First number to find Average = "); scanf("%d",&no1); //Taking the input number 2 from the user. printf("Enter the Second number to find Average = "); scanf("%d",&no2); //adding those two numbers int sum = no1 + no2; //Finding the average of the those numbers average = sum/2; //(float)sum/2; //Printing the result output of the program printf("The Sum of %d and %d = %d\n", no1, no2, sum); printf("The Average of %d and %d = %.2f\n", no1, no2, average); return 0; } |
Output:-
In the above program, we have first initialized the required variable.
- no1 = it will hold the integer value.
- no2 = it will hold the integer value.
- sum = it will hold the integer value.
- average = it will hold the float value.
Taking input number from the user to find the cube.
Calculating the average of those numbers.
Printing output average for that given numbers.