How to write a C Program to calculate the Sum and Average of N number using For Loop, While Loop and Do While Loop.

## C Program to find Sum and Average of n Number using For Loop

This C program allows the user to enter the number (n) he wishes to calculate the average and sum. Next, it will ask the user to enter individual items up to a declared number.

Using the For loop in C Programming, this program will calculate the sum, and later this C program will calculate the average.

#include<stdio.h> int main() { int i,n,Sum=0,numbers; float Average; printf("\nPlease Enter How many Number you want?\n"); scanf("%d",&n); printf("\nPlease Enter the elements one by one\n"); for(i=0;i<n;++i) { scanf("%d",&numbers); Sum = Sum +numbers; } Average = Sum/n; printf("\nSum of the %d Numbers = %d",n, Sum); printf("\nAverage of the %d Numbers = %.2f",n, Average); return 0; }

```
Please Enter How many Number you want?
2
Please Enter the elements one by one
10
20
Sum of the 2 Numbers = 30
Average of the 2 Numbers = 15.00
```

The code analysis behind this C program to find Average of n Number

- The first printf statement will ask the user to enter n value. For example, if the user enters 2, then the second printf statement will ask the user to enter those two values, one after the other.
- For loop will resist the user, not to enter more than two values by using the condition i<n.
- In the next line, we added the entered value to sum.
- After completing this, it will start the second iteration. For the 3rd iteration, the condition (i<n) will fail so, it will exit from the For loop.
- Outside the loop, we calculated the average using the formula sum/n. In our C Programming example, it is 30/2 = 15

## C Program to find Sum and Average of n Number using While Loop

This program allows the user to enter the number (n) he wishes to calculate the average and sum. Next, it will ask the user to enter individual items up to a declared number. Using the While Loop, it will calculate the sum, and later it will calculate the average.

#include<stdio.h> void main() { int n, numbers, i=0,Sum=0; float Average; printf("\nPlease Enter How many Number you want?\n"); scanf("%d",&n); printf("\nPlease Enter the elements one by one\n"); while(i<n) { scanf("%d",&numbers); Sum = Sum +numbers; i++; } Average = Sum/n; printf("\nSum of the %d Numbers = %d",n, Sum); printf("\nAverage of the %d Numbers = %.2f",n, Average); return 0; }

Within this C Program to Calculate the Sum and Average of n Number, the first printf statement will ask the user to enter n value. For example, If the user enters 5, then the second printf statement will ask the user to enter those 5 values one after the other.

The while loop will resist the user, not to enter more than 2 values by using the condition i<n.

In the next line, we added the entered value to the sum. After completing this, the value of i incremented by the Increment operator.

Next, it will start the second iteration. For the 3rd iteration the condition (i<n) will fail. So, it will exit from the loop.

## C Program to find Sum and Average of n Number using Do While Loop

This program allows the user to enter the number (n) he wishes to calculate the average and sum. Next, it will ask the user to enter individual items up to a declared number. Using the Do While Loop, it will calculate the sum and later calculates the average.

#include<stdio.h> int main() { int n, numbers, i=0,Sum=0; float Average; printf("\nPlease Enter How many Number you want?\n"); scanf("%d",&n); printf("\nPlease Enter the elements one by one\n"); do { scanf("%d",&numbers); Sum = Sum +numbers; i++; }while(i<n); Average = Sum/n; printf("\nSum of the %d Numbers = %d",n, Sum); printf("\nAverage of the %d Numbers = %.2f",n, Average); return 0; }

```
Please Enter How many Number you want?
3
Please Enter the elements one by one
20
30
50
Sum of the 3 Numbers = 100
Average of the 3 Numbers = 33.00
```

We just replaced the While loop with the Do While loop. Please don’t forget to miss the semi-colon after the while condition.

Comments are closed.