In this C++ program, you will learn to find the arithmetic mean of all the numbers entered by the user in C++.
Arithmetic mean can be found out by adding all the numbers and dividing that sum by the count of numbers. For example, the mean of the numbers 5, 7, 9 is 7 since 5 + 7 + 9 = 21 and 21 divided by 3 [there are three numbers] is 7.
To calculate the arithmetic mean of numbers in C++ programming, the user is asked to enter the number of integers. Get the number into the variable n. and then ask to enter the integers to find and print the arithmetic mean. Get the entered n numbers into an array arr[50]. A for loop can be used to read the entered numbers into the array arr[50].
Set a value to get the sum of the entered n numbers sum. Initially, the value of the sum is set to 0. Now the sum of the entered numbers is stored in the variable sum, to find the arithmetic mean, divide the sum by the number of elements n.
armean = sum/n;
Display the result on the screen
Step 1: Call the header file iostream.
Step 2: Use the namespace std.
Step 3: Open the main function; int main().
Step 4: Declare integer type variables n, I and float type array arr[50], sum, armean;
Step 5: set the value of the variable sum to 0 initially;
Step 6: Ask the user to enter the number of elements;
Step 7: get the number to the variable n;
Step 8: Ask the user to enter the elements;
Step 9: Read the entered numbers into the array arr[50] by using a for loop;
Step 10: calculate the same by using the formula sum = sum + arr[i] within the for loop
;
Step 11: Calculate the arithmetic mean by using the equation sum/n;
Step 12: Get the result into the variable armean
Step 13: Display the result;
Step 14:Exit
#include<iostream>
using namespace std;
int main()
{
int n, i;
float arr[50], sum=0, armean;
cout<<"How many number, You want to enter ? ";
cin>>n;
cout<<"\nEnter "<<n<<" Number: ";
for(i=0; i<n; i++)
{
cin>>arr[i];
sum = sum+arr[i];
}
armean = sum/n;
cout<<"\nArithmetic Mean = "<<armean;
cout<<endl;
return 0;
}
How many number, You want to enter ? 5 Enter 5 Number: 10 23 54 63 2 Arithmetic Mean = 30.4