w3resource

C Exercises: Read 10 numbers and find their sum and average

C For Loop: Exercise-4 with Solution

Write a program in C to read 10 numbers from the keyboard and find their sum and average.

Visual Presentation:

Read 10 numbers and find their sum and average

Pseudo code:

initialize sum = 0
initialize ctr = 0
for i = 1 to 10 do
   read number[i]
   sum = sum + number[i]
   ctr = ctr + 1
end for
average = sum / ctr
print "Sum of the 10 numbers is ", sum
print "Average of the 10 numbers is ", average

Sample Solution:

C Code:

#include <stdio.h>  // Include the standard input/output header file.
int main() {
    int i, n, sum = 0;  // Declare variables 'i' for loop counter, 'n' for user input, 'sum' to store the sum.
    float avg;  // Declare variable 'avg' to store the average.

    printf("Input the 10 numbers : \n");  // Print a message to prompt user input.

    for (i = 1; i <= 10; i++) {  // Start a for loop to iterate 10 times.
        printf("Number-%d :", i);  // Print a message to indicate which number is being input.

        scanf("%d", &n);  // Read the value of 'n' from the user.
        sum += n;  // Add the value of 'n' to the running sum.
    }

    avg = sum / 10.0;  // Calculate the average.

    printf("The sum of 10 no is : %d\nThe Average is : %f\n", sum, avg);  // Print the sum and average.
    return 0;
}


Sample Output:

Input the 10 numbers :                                                                                        
Number-1 :1                                                                                                   
Number-2 :2                                                                                                   
Number-3 :3                                                                                                   
Number-4 :4                                                                                                   
Number-5 :5                                                                                                   
Number-6 :6                                                                                                   
Number-7 :7                                                                                                   
Number-8 :8                                                                                                   
Number-9 :9                                                                                                   
Number-10 :10                                                                                                 
The sum of 10 no is : 55                                                                                      
The Average is : 5.500000  

Explanation:

for (i = 1; i <= 10; i++) {
  printf("Number-%d :", i);

  scanf("%d", & n);
  sum += n;
}

In the above for loop, the variable i is initialized to 1, and the loop will continue as long as i is less than or equal to 10. In each iteration of the loop, the printf() function will print the string "Number-" followed by the value of i and a colon (:) to the console.

Then, the scanf() function will wait for the user to input an integer value, which will be stored in the variable 'n'. The value of 'n' will then be added to the variable 'sum' in each iteration of the loop.

Finally, the loop will increment the value of i by 1, and the process will repeat until the condition i<=10 is no longer true.

Flowchart:

Flowchart: Read 10 numbers and find their sum and average

C Programming Code Editor:

Previous: Write a program in C to display n terms of natural number and their sum.
Next: Write a program in C to display the cube of the number upto a given integer.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.