- C++ Programming Examples
- C++ Programming Examples
- C++: Hello World
- C++: Get Input
- C++: Print Integer
- C++: Add two numbers
- C++: Add, Sub, Multiply, Div
- C++: Add Digits
- C++: Find Average and Percentage
- C++: Find Arithmetic Mean
- C++: Sum of n Natural Numbers
- C++: Sum of n Numbers
- C++: Square's Area and Perimeter
- C++: Rectangle's Area and Perimeter
- C++: Triangle's Area and Perimeter
- C++: Area and Circumference
- C++: Find Simple Interest
- C++: Fahrenheit to Celsius
- C++: Celsius to Fahrenheit
- C++: Print Prime Numbers
- C++: Reverse a Number
- C++: Swap Two Numbers
- C++: Print Multiplication Table
- C++: Find Factorial of a Number
- C++: Find Factors of a Number
- C++: Find HCF and LCM
- C++: Create a Calculator
- C++: Count Digits in a Number
- C++: First and Last Digit Sum
- C++: Product of Number Digits
- C++: Sum of Squares of Digits
- C++: Interchange Digits of Number
- C++ if-else Programs
- C++: Check Even or Odd
- C++: Check Prime or Not
- C++: Check Alphabet or Not
- C++: Check Vowel or Not
- C++: Check Leap Year or Not
- C++: Check Reverse equals Original
- C++: Check Perfect Number
- C++: Check Palindrome or Not
- C++: Check Armstrong or Not
- C++: Divisibility Test
- C++: Find Labor Wage
- C++: Find Discounted Price
- C++: Find Shipping Charge
- C++: Find Telephone Bills
- C++: Calculate Student Grade
- C++: Largest of Two Numbers
- C++: Largest of Three Numbers
- C++ Number Conversion
- C++: Decimal to Binary
- C++: Decimal to Octal
- C++: Decimal to Hexadecimal
- C++: Binary to Decimal
- C++: Binary to Octal
- C++: Binary to Hexadecimal
- C++: Octal to Decimal
- C++: Octal to Binary
- C++: Octal to Hexadecimal
- C++: Hexadecimal to Decimal
- C++: Hexadecimal to Binary
- C++: Hexadecimal to Octal
- C++ Pattern Programs
- C++: Pattern Programs
- C++: Print Diamond Pattern
- C++: Print Floyd's Triangle
- C++: Print Pascal's Triangle
- C++ Array Programs
- C++: 1D Array Program
- C++: Linear Search
- C++: Binary Search
- C++: Largest Element in an Array
- C++: Smallest Element in an Array
- C++: Find Second Largest Element
- C++: Find Second Smallest Element
- C++: Sum of All Elements
- C++: Multiply All Elements
- C++: Element in Even Position
- C++: Element in Odd Position
- C++: Print Even Numbers in Array
- C++: Print Odd Numbers in Array
- C++: Count Even or Odd Numbers
- C++: Sum of Even or Odd Numbers
- C++: Count Positive, Negative, Zero
- C++: Reverse an Array
- C++: Insert an Element
- C++: Delete an Element
- C++: Merge two Arrays
- C++: Bubble Sort
- C++: Selection Sort
- C++: Insertion Sort
- C++: Common Elements
- C++: 2D Array Programs
- C++: Add Two Matrices
- C++: Subtract Two Matrices
- C++: Transpose Matrix
- C++: Multiply Two Matrices
- C++: 3D Array Programs
- C++ String Programs
- C++: Print String
- C++: Find String Length
- C++: Compare Two Strings
- C++: Copy String
- C++: String Concatenation
- C++: Reverse a String
- C++: Delete Vowels from a String
- C++: Delete a Word from a String
- C++: Count Characters in a String
- C++: Count Words in a String
- C++: Frequency of Words
- C++: Remove Spaces from Strings
- C++: Sort a String
- C++: Uppercase to Lowercase
- C++: Lowercase to Uppercase
- C++: Swap Two Strings
- C++: Check the Anagram or Not
- C++: Capitalize All Words in a String
- C++: Get Numbers from a String
- C++ File Programs
- C++: Read a File
- C++: Write Content to a File
- C++: Append Data to a File
- C++: Read and Display File
- C++: Copy a File
- C++: Merge Two Files
- Count Characters in a File
- C++: Capitalize Every Word
- C++: List Files in Directory
- C++: Delete a File
- C++: Encrypt and Decrypt a File
- C++ Misc Programs
- C++: Print ASCII Value
- C++: Add Binary Numbers
- C++: Generate Random Numbers
- C++: Print a Smiling Face
- C++: Days into Years and Months
- C++: Add Two Numbers using Pointer
- C++: Print Fibonacci Series
- C++: Generate Armstrong Numbers
- C++: Find nCr and nPr
- C++: Get IP Address
- C++: Print Date and Time
- C++: Shutdown and Restart Computer
- C++ Programming Tutorial
- C++ Tutorial
C++ Program to Calculate Arithmetic Mean
In this article, you will learn and get code on finding the arithmetic of all the numbers entered by the user in C++. The user is also allowed to define the size. For example, if the user enters 5 as the size, then the program further asks for any 5 numbers to find the arithmetic mean. There are two programs available here:
- Calculate the arithmetic mean without using a function
- Find the arithmetic mean using a user-defined function
How to find the arithmetic mean
If there are n sets of numbers, we have a formula for calculating the arithmetic mean:
am = (n1+n2+n3+...+nn)/n
Here am indicates the arithmetic mean, whereas n1, n2, n3, and nn indicate the first, second, third, and last number, respectively.
For example, if we have five sets of numbers, say 10, 20, 30, 40, and 50, we can calculate their arithmetic mean as follows:
am = (10+20+30+40+50)/5 = 150/5 = 30
Now let's move on to the program.
In C++, find the Arithmetic Mean
In order to calculate (or find) the arithmetic mean (of numbers) in C++ programming, you must first ask the user to enter the size (how many sets of numbers) and then ask the user to enter all numbers of that size in order to find and print the arithmetic mean.
To calculate the arithmetic mean of numbers, first perform the addition of all the numbers, then make a variable responsible for the arithmetic mean and place addition or size in a variable saying "armean" (arithmetic mean), then display the result on the output screen as shown here in the following program.
#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; }
This program was built and runs under the Code::Blocks IDE. Here is its sample run:
Now enter the size, which is how many numbers you want to enter to find the arithmetic mean. Let's suppose, user enters the size as 5 and the 5 numbers as 10, 2, 4, 6, and 12. Press the ENTER key to see the following output:
When we received the numbers, we added them one by one to the total. For instance, if the user enters 5 as the size and the numbers 10, 2, 4, 6, and 12, the dry run (within the for loop) will look like this:
- Initially, 0 gets initialized to i. So i=0
- Checks the condition to see if i (0) is less than n (5).
- The condition evaluates to be true, therefore program flow goes inside the loop and receives a number from the user and stores it in arr[i] (that is, arr[0]).
- And the statement sum = sum+arr[i] or sum = 0+arr[0] or sum = 0+10 (assuming the first number is 10). Therefore, sum=10
- The program flow now moves to the for loop's updating section and increments the value of i. Now i=1
- Checks the condition because condition was evaluated as true again, so program flow returns to the loop and evaluates two statements.
- that is, receives a number, say 2, and stores it in arr[1].
- Similarly, sum+arr[1] or 10+2 is initialized to sum. Now sum=12
- Continue until the value of i equals 5 or the condition i<n evaluates to false.
- After exiting from the loop, we'll have a variable sum that holds the sum of all the 5 numbers.
- Now apply the final formula of arithmetic mean, which is sum/size, which is sum/5. Put the sum value. The variable armean holds its value (sum/size). Therefore, just print the value of armean as output, that will be the arithmetic mean.
In C++, find the arithmetic mean using the function
This function does the same job as the previous program. But it is created using functions. That is, a function like arithmeticMean() takes two arguments: the first one is the array, and the second is its size. and returns the arithmetic mean of all numbers stored in the array.
#include<iostream> using namespace std; float arithmeticMean(float [], int); int main() { int n, i; float arr[50], armean; cout<<"Enter the Size (maz. 50): "; cin>>n; cout<<"\nEnter "<<n<<" Numbers: "; for(i=0; i<n; i++) cin>>arr[i]; armean = arithmeticMean(arr, n); cout<<"\nArithmetic Mean = "<<armean; cout<<endl; return 0; } float arithmeticMean(float arr[], int n) { int i; float sum=0, am; for(i=0; i<n; i++) sum = sum+arr[i]; am = sum/n; return am; }
Here is its sample run with user input of 2 as size and 10, 3, as two numbers:
The same program in different languages
« Previous Program Next Program »