Program to print the average of n numbers

The average is the outcome from the sum of the numbers divided by the count of the numbers being averaged.

For example: 1,2,3,4,5

Number of all elements = 5

Sum of all elements = 1+2+3+4+5 =15

Average = Sum of all elements / number of all elements = 15/5 =3

Average =3

Algorithm

  • STEP 1: START
  • STEP 2: DEFINE n.
  • STEP 3: SET count = 1
  • STEP 4: DEFINE xF, averageF
  • STEP 5: SET sumF = 0
  • STEP 6: ENTER n
  • STEP 7: REPEAT STEP 8 to 10 UNTIL count<=n
  • STEP 8: Enter xF
  • STEP 9: sumF += xF
  • SETP 10: count = count + 1
  • SETP 11: SET averageF = sumF/(n-1)
  • STEP 12: PRINT averageF
  • STEP 13: END

Java Program

Output:

Enter the value of n
5
Enter the 1 number?
1
Enter the 2 number?
2  
Enter the 3 number?
3
Enter the 4 number?
4
Enter the 5 number?
5
The Average is 3.0

C program

Output:

Enter the value of n? 5
Enter the 1 number? 1
Enter the 2 number? 2
Enter the 3 number? 3
Enter the 4 number? 4
Enter the 5 number? 5

The Average is 3.0000

Python program

Output:

Enter the value of n? 5
Enter the 1 number? 1
Enter the 2 number? 2
Enter the 3 number? 3
Enter the 4 number? 4
Enter the 5 number? 5
Average using a while loop   3.0000

Php program

Output:

Enter the value of n? 5
Enter the number 1
Enter the number 2
Enter the number 3
Enter the number 4
Enter the number 5
3.0000

C# program

Output:

Enter the value of n?
5
Enter the 1 number?
1
Enter the 2 number?
2  
Enter the 3 number?
3
Enter the 4 number?
4
Enter the 5 number?
5
The Average is 3.0
Next Topic#




Latest Courses