First printf statement will ask the user to enter n value. For example, if the user enters 2 then, second printf statement will ask the user to enter those 2 values one after the other.

For 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 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 loop.

Outside the loop, we calculated the average using the formula sum/n. In our 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 item up to a declared number. Using the While Loop it will calculate the sum and later it will calculate the average.

First printf statement will ask the user to enter n value. For example, If the user enters 5 then, second printf statement will ask the user to enter those 5 values one after the other.

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 sum. After completing this, the value of i is increment by the Increment operator.

Now 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 item up to a declared number. Using the Do While Loop it will calculate the sum and later it will calculate the average.