In this post, we will learn how to read 10 numbers and find their sum and average using C Programming language.
This program asks the user to enter 10 number, then it computes the sum and average of those 10 numbers using following approaches:
- Using For Loop
- Using While Loop
- Using Do While Loop
So, without further ado, let’s begin this tutorial.
C Program to Read 10 Numbers and Find their Sum and Average
// C Program to Read 10 Numbers and Find their Sum and Average #include <stdio.h> int main(){ int num, sum = 0; float avg; // Asking for input printf("Enter the 10 numbers: \n"); for (int i = 1; i <= 10; i++){ printf("Number %d = ", i); scanf("%d", &num); sum = sum + num; } // Calculating average avg = sum / 10; // Displaying output printf("Sum of 10 numbers: %d\n", sum); printf("Average of 10 numbers: %.2f", avg); return 0; }
Output
Enter the 10 numbers:
Number 1 = 5
Number 2 = 13
Number 3 = 11
Number 4 = 15
Number 5 = 7
Number 6 = 18
Number 7 = 21
Number 8 = 33
Number 9 = 9
Number 10 = 20
Sum of 10 numbers: 152
Average of 10 numbers: 15.00
How Does This Program Work ?
int num, sum = 0; float avg;
In this program, we have declared two int data type variables named num, sum and one float data type variable named avg.
// Asking for input printf("Enter the 10 numbers: \n"); for (int i = 1; i <= 10; i++){ printf("Number %d = ", i); scanf("%d", &num); sum = sum + num; }
Then, the user is asked to enter 10 numbers. Here, we have used for loop to take 10 numbers as input.
// Calculating average avg = sum / 10;
We calculate the average of 10 numbers by dividing the total sum by the total number of terms (which is 10 in this case).
// Displaying output printf("Sum of 10 numbers: %d\n", sum); printf("Average of 10 numbers: %.2f", avg);
Finally, the sum and average of 10 numbers is displayed on the screen using printf() function.
C Program to Read 10 Number and Find their Sum and Average Using While Loop
// C Program to Read 10 Numbers and Find their Sum and Average Using While Loop #include <stdio.h> int main(){ int num, i = 1, sum = 0; float avg; // Asking for input printf("Enter 10 numbers: \n"); while (i <= 10){ printf("Number %d = ", i); scanf("%d", &num); sum = sum + num; i++; } // Computing the average avg = sum / 10; // Display output printf("Sum of 10 numbers: %d\n", sum); printf("Average of 10 numbers: %.2f", avg); return 0; }
Output
Enter 10 numbers:
Number 1 = 7
Number 2 = 3
Number 3 = 1
Number 4 = 5
Number 5 = 9
Number 6 = 6
Number 7 = 14
Number 8 = 20
Number 9 = 21
Number 10 = 25
Sum of 10 numbers: 111
Average of 10 numbers: 11.00
C Program to Read 10 Numbers and Find their Sum and Average Using Do While Loop
// C Program to Read 10 Numbers and Find their Sum and Average Using Do While Loop #include <stdio.h> int main(){ int num, i = 1, sum = 0; float avg; // Asking for input printf("Enter the 10 numbers: \n"); do{ printf("Number %d = ", i); scanf("%d", &num); sum = sum + num; i++; } while (i <=10); // Calculating average avg = sum / 10; // Display output printf("Sum of 10 numbers: %d\n", sum); printf("Average of 10 numbers: %.2f", avg); return 0; }
Output
Enter the 10 numbers:
Number 1 = 5
Number 2 = 7
Number 3 = 9
Number 4 = 11
Number 5 = 2
Number 6 = 14
Number 7 = 17
Number 8 = 30
Number 9 = 21
Number 10 = 11
Sum of 10 numbers: 127
Average of 10 numbers: 12.00
Conclusion
I hope after going through this post, you understand how to read 10 numbers and find their sum and average 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 assist you.
Also Read: