In this program, we will learn how to find the sum and average of 3 numbers using C Programming language.
This program asks the user to enter three integers, then it computes the sum and average of those three numbers using simple arithmetic operators.
So, without further ado, let’s begin this tutorial.
C Program to Find Sum and Average of 3 Numbers
// C Program to Find Sum and Average of 3 Numbers #include <stdio.h> int main(){ int a, b, c, sum; float avg; // Asking for input printf("Enter 3 numbers: \n"); scanf("%d %d %d", &a, &b, &c); // Calculating sum sum = a + b + c; // Calculating average of 3 numbers avg = sum / 3; // Displaying output printf("Sum = %d \n", sum); printf("Average = %.2f", avg); return 0; }
Output
Enter 3 numbers:
3
5
7
Sum = 15
Average = 5.00
How Does This Program Work ?
int a, b, c, sum; float avg;
In this program, we have declared four int data type variables named a, b, c and sum and one float data type variable named avg.
// Asking for input printf("Enter 3 numbers: \n"); scanf("%d %d %d", &a, &b, &c);
Then, the user is asked to enter 3 numbers. The input is taken with the help of scanf() function.
// Calculating sum sum = a + b + c;
We calculate the sum of three numbers using the plus (+) operator.
// Calculating average of 3 numbers avg = sum / 3;
Similarly, the average of these three numbers is calculated using the formula:
Average = Total Sum / Total no. of terms
// Displaying output printf("Sum = %d \n", sum); printf("Average = %.2f", avg);
Finally, the result is displayed on the screen using printf() function. Here, we have used %.2f format specifier because we want to display the average only till 2 decimal places.
Conclusion
I hope after going through this post, you understand how to find the sum and average of 3 numbers using C Programming language.
If you have any doubt regarding the program, feel free to contact us in the comment section. We will be delighted to solve your query.
Also Read:
It is really helpfull for the students.
It is very useful for students.