Write a program to find the average of list of numbers entered by the user

By | October 4, 2021

Problem Statement

We need to write a script or program that accepts n number of numbers from the user and prints their average. The program should be designed in such a way so it first asks the user to enter the number of elements or values that the user wants to enter, into the list.

Solution 

The flow of the program should be as follow:

Vamware

First, we will ask the user to enter the number of elements n he/she wants to enter. Then create a for loop from range 1 to n, and inside the loop, we will keep asking the user to enter the value or element, and simultaneously add and store the value in a variable called sum. And at last calculate the average by dividing the total sum by the entered number n.

C program to find the average of a list of numbers entered by the user

#include <stdio.h>

int main()
{	
	int num, ele, i, sum=0, average;
	
	//input the number
	printf("How many elements you want to enter?: ");
	scanf("%d",&num);
	
	for(i=1; i<=num; i++)
	{
		printf("Enter Number %d: ", i);
		scanf("%d", &ele);
		
		//add the enterd number into total sum
		sum +=ele;
	}
	
	//compute the average
	average = sum /num;
	
	printf("The average of the above entered numbers is %d", average);
	

	return 0;
}

Output

How many elements you want to enter?: 6
Enter Number 1: 89
Enter Number 2: 80
Enter Number 3: 90
Enter Number 4: 78
Enter Number 5: 86
Enter Number 6: 76
The average of the above entered numbers is 83.00

C++ program to find the average of a list of numbers entered by the user

#include<iostream>

using namespace std;
int main()
{
	int num, ele, i, sum=0, average;
	
	//input the number
	cout<<"How many elements you want to enter?: "; cin>>num;
	
	for(i=1; i<=num; i++)
	{
		cout<<"Enter Number "<< i<<" :"; cin>>ele;
		
		//add the enterd number into total sum
		sum +=ele;
	}
	
	//compute the average
	average = sum /num;
	
	cout<<"The average of the above entered numbers is "<< average;
	

	return 0; 
}

Output

How many elements you want to enter?: 6
Enter Number 1 :34
Enter Number 2 :44
Enter Number 3 :25
Enter Number 4 :26
Enter Number 5 :54
Enter Number 6 :36
The average of the above entered numbers is 36

Python:

# input the number
num = int(input("How many elements you wants to enter?: "))
sum_=0

for i in range(1, num+1):
    ele = int(input(f"Enter Number {i}: "))

    # add the entered number into the total sum
    sum_ +=ele

# compute the average
average = sum_/num

print("The average of the above entered numbers is:", round(average,2))

Output:

How many elements you wants to enter?: 5
Enter Number 1: 234
Enter Number 2: 353
Enter Number 3: 644
Enter Number 4: 234
Enter Number 5: 355
The average of the above entered numbers is: 364.0

Final Thoughts!

The above problem statement is very easy, and so does its implementation. All you need to do is keep adding the newly entered number to the total sum. It’s very important to initialize an initial variable sum with value 0, because if we only declare the variable without an initial value, the compiler may give some garbage value to the variable that would affect the output. To solve the above problem we could also have used an array to store all the elements, but it would just increase the space complexity of the program.

If you like this article or have any suggestions please comment down below and let us know.

People are also reading:

Leave a Reply

Your email address will not be published. Required fields are marked *