- How to read 10 numbers from the keyboard using for loop in C
- How to calculate the sum of 10 numbers
- How to calculate the average of 10 numbers
- How to display the sum and average using printf()
Introduction
In this tutorial, we'll learn how to write a C program using a for loop to read 10 numbers from the keyboard and find their sum and average. This is a fundamental program that helps you understand how to use loops for repeated input operations and arithmetic calculations.
The program uses a for loop to iterate 10 times, each time taking a number input from the user. It adds each number to a running total, and after the loop completes, it calculates the average by dividing the total by 10.
š” Key Concept: The for loop is ideal when you know the exact number of iterations. Here, we need exactly 10 inputs, so the for loop is a perfect choice.
Program Code
C Program to Find Sum and Average of 10 Numbers Using For Loop
#include <stdio.h>
int main() {
int i, total = 0, n;
float avg = 0.0;
printf("Enter any 10 Numbers\n");
for (i = 1; i <= 10; i++) {
scanf("%d", &n);
total += n;
}
avg = total / 10.0;
printf("\nTotal = %d", total);
printf("\nAverage = %.2f", avg);
return 0;
}
Program Output
Enter any 10 Numbers
11
10
6
4
9
8
3
7
4
10
Total = 72
Average = 7.20
Program Explanation
Let's break down this program step by step to understand how it works:
1. Header File Inclusion:
#include <stdio.h>
This includes the standard input/output library, which provides functions like printf() and scanf().
2. Variable Declaration:
int i, total = 0, n;
float avg = 0.0;
i is the loop counter, total stores the sum of all numbers (initialized to 0), n stores each input number, and avg stores the average (initialized to 0.0).
3. User Prompt:
printf("Enter any 10 Numbers\n");
This displays a message asking the user to enter 10 numbers.
4. For Loop:
for (i = 1; i <= 10; i++) {
scanf("%d", &n);
total += n;
}
The loop runs 10 times (i from 1 to 10). In each iteration:
scanf("%d", &n)reads a number from the user and stores it inntotal += nadds the number to the running total
5. Average Calculation:
avg = total / 10.0;
The average is calculated by dividing the total by 10.0 (using 10.0 instead of 10 ensures floating-point division).
6. Display Result:
printf("\nTotal = %d", total);
printf("\nAverage = %.2f", avg);
The program prints the total and average. %.2f formats the average to 2 decimal places.
7. Return Statement:
return 0;
Indicates that the program executed successfully and returns control to the operating system.
Practice Exercises
š Exercise 1: Sum and Average of N Numbers
Modify the program to read N numbers (where N is provided by the user) and find their sum and average.
Show Solution
#include <stdio.h>
int main() {
int i, n, total = 0, num;
float avg;
printf("How many numbers? ");
scanf("%d", &n);
for(i = 1; i <= n; i++) {
printf("Enter number %d: ", i);
scanf("%d", &num);
total += num;
}
avg = total / (float)n;
printf("\nTotal = %d", total);
printf("\nAverage = %.2f", avg);
return 0;
}
š Exercise 2: Sum and Average Using While Loop
Rewrite the program using a while loop instead of a for loop.
Show Solution
#include <stdio.h>
int main() {
int i = 1, total = 0, n;
float avg = 0.0;
printf("Enter any 10 Numbers\n");
while(i <= 10) {
scanf("%d", &n);
total += n;
i++;
}
avg = total / 10.0;
printf("\nTotal = %d", total);
printf("\nAverage = %.2f", avg);
return 0;
}
Frequently Asked Questions
1. Why do we use a for loop to read 10 numbers?
We use a for loop because we know exactly how many numbers we need to read (10). The for loop provides a clean and structured way to repeat a block of code a specific number of times.
2. Why do we divide by 10.0 instead of 10?
Dividing by 10.0 ensures floating-point division. If we divide by 10 (an integer), the result would be an integer (integer division), which would truncate the decimal part.
3. What happens if the user enters a non-integer value?
If the user enters a non-integer value, the scanf() function will fail and the program may produce unexpected results. In a production program, you would add input validation to handle such cases.
4. Can we use a do-while loop for this program?
Yes, you can use a do-while loop. However, a for loop is more appropriate here because we know exactly 10 iterations are needed. A do-while loop is better when the number of iterations is unknown or when you need to execute at least once.
š” Tip: Always initialize your variables before using them. In this program, total and avg are initialized to 0 to avoid garbage values.
šÆ Key Takeaway: The for loop is perfect for situations where you know the exact number of iterations. This program demonstrates how to read multiple inputs and perform calculations, which is a fundamental skill in C programming.
š„ For Loop Assignments (Practice Set 1)
Practice the most important C for loop programs asked in exams and interviews.
-
1. Display First N Natural Numbers
Write a program in C using for loop to display the first N natural numbers.
-
2. Print Odd Numbers from 1 to N
Write a program in C to print ODD numbers from 1 to N using for loop.
-
3. Print Even Numbers from 1 to N
Write C program to show EVEN numbers from 1 to N using for loop.
-
4. Print Uppercase Alphabets
Write a program in C to print all uppercase alphabets using for loop.
-
5. Print Lowercase Alphabets
Write a program in C to display all lowercase alphabets using for loop.
-
6. Sum and Average of 10 Numbers
Write a program in C using for loop to read 10 numbers and find their sum and average.
-
7. Multiplication Table
Write C program to read an integer and print its multiplication table using for loop.
-
8. Positive, Negative or Zero Check
C program to check entered number is ZERO, POSITIVE or NEGATIVE using for loop.
-
9. Factorial of a Number
Write a program in C to find factorial of a number using for loop.
-
10. Sum and Average of N Numbers
Find sum and average of first N natural numbers using for loop.
š For Loop Assignments (Practice Set 2)
- 1. Reverse a Number - Reverse a given number using for loop.
- 2. Armstrong Number - Check if a number is Armstrong using for loop.
- 3. Fibonacci Series - Print Fibonacci series up to N using for loop.
- 4. Prime Number Check - Check if a number is prime using for loop.
- 5. Decimal to Binary - Convert decimal to binary using for loop.
- 6. Sum of Squares - 1²+2²+3²+...+N² using for loop.
- 7. Sum of Factorial Series - 1/1! + 2/2! + ... + N/N! using for loop.
- 8. Harmonic Series - 1 + 1/2 + 1/3 + ... + 1/N using for loop.
Other Tutorials
SQL FAQ
Java FAQ
Python FAQ