- C++ Programming Examples
- C++ Programming Examples
- C++ Hello World
- C++ Get Input
- C++ Print Integer
- C++ Add Two Numbers
- C++ Add Sub Mul Div
- C++ Add Digits
- C++ Find Average Perc
- C++ Find Arithmetic Mean
- C++ Sum of n Natural Numbers
- C++ Sum of n Numbers
- C++ Area Perimeter of Square
- C++ Area Perimeter of Rectangle
- C++ Area Perimeter of Triangle
- C++ Area Circum of Circle
- 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 Number
- C++ Find Factors of Number
- C++ Find HCF & LCM
- C++ Make Calculator
- C++ Count Digits in Number
- C++ Sum of First & Last Digit
- C++ Product of Digits of Number
- 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
- Check Reverse equal Original
- C++ Check Perfect Number
- C++ Check Palindrome or Not
- C++ Check Armstrong or Not
- C++ Divisibility Test
- C++ Find Wage of Labor
- 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
- Find Largest Element in Array
- Find Smallest Element in Array
- Find Second Largest Element
- Find Second Smallest Element
- C++ Sum of All Elements
- C++ Multiply All Elements
- C++ Element on Even Position
- C++ Element on Odd Position
- C++ Print Even Numbers in Array
- C++ Print Odd Numbers in Array
- C++ Count Even/Odd Numbers
- C++ Sum of Even/Odd Numbers
- C++ Count Positive Negative Zero
- C++ Reverse an Array
- C++ Insert Element in Array
- C++ Delete Element from Array
- 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 Length of String
- C++ Compare Two Strings
- C++ Copy String
- C++ Concatenate String
- C++ Reverse a String
- C++ Delete Vowels from String
- C++ Delete Word from String
- C++ Count Character in String
- C++ Count Word in String
- C++ Frequency of Word
- C++ Remove Spaces from String
- C++ Sort a String
- C++ Uppercase to Lowercase
- C++ Lowercase to Uppercase
- C++ Swap Two Strings
- C++ Check Anagram or Not
- C++ Capitalize All Words in String
- C++ Capitalize Specific Character
- C++ Get Numbers from String
- C++ File Programs
- C++ Read a File
- C++ Write Content to File
- C++ Append Data in File
- C++ Read & Display File
- C++ Copy a File
- C++ Merge Two Files
- Count Characters, Words in File
- C++ Capitalize All Words in File
- C++ List Files in Directory
- C++ Delete a File
- C++ Encrypt & Decrypt a File
- C++ Misc Programs
- C++ Print ASCII Value
- C++ Add Binary Numbers
- C++ Generate Random Numbers
- C++ Print Smiling Face
- C++ Days into Years, Months
- Add Two Numbers using Pointer
- C++ Print Fibonacci Series
- Generate Armstrong Numbers
- C++ Find nCr and nPr
- C++ Get IP Address
- C++ Print Date/Time
- C++ Shutdown, Restart Computer
- C++ Programming Tutorial
- C++ Tutorial
- C++ Programming Test
- C++ Programming Test
- Give Online Test
- All Test List
C++ Program to Calculate Arithmetic Mean
In this article, you will learn and get code on finding arithmetic of all the numbers entered by user in C++. User is also allowed to define the size. For example, if user enters 5 as the size, then program further asks to enter any 5 numbers to find arithmetic mean. There are two programs available here:
- Find Arithmetic Mean without using Function
- Find Arithmetic Mean using user-defined Function
How to Find Arithmetic Mean ?
If there are n set of numbers, therefore to find arithmetic mean, we have a formula:
am = (n1+n2+n3+...+nn)/n
Here am indicates arithmetic mean, whereas n1, n2, n3, and nn indicates first, second, third, and last number.
For example, if we have 5 set of numbers say 10, 20, 30, 40, 50, then its arithmetic means can be calculated as:
am = (10+20+30+40+50)/5 = 150/5 = 30
Now let's move on to the program.
Find Arithmetic Mean in C++
To calculate (or find) arithmetic mean (of numbers) in C++ programming, you have to ask from user to enter the size (how many set of number), then ask to enter all numbers of that size to find and print arithmetic mean.
To calculate arithmetic mean of numbers, first perform addition of all the numbers, then make a variable responsible for the arithmetic mean and place addition/size in a variable say 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 build and run under Code::Blocks IDE. Here is its sample run:
Now enter the size, that is how many numbers you want to enter find the arithmetic mean. Let's suppose, user
enters the size as 5 and 5 numbers as 10, 2, 4, 6, 12. Press ENTER
key to see the
following output:
At the time of receiving numbers, we've added each, one by one to sum. For example, if user enters 5 as size, and its numbers as 10, 2, 4, 6, 12, then dry run (inside the for loop) goes like:
- Initially 0 gets initialized to i. So i=0
- Checks the condition, whether i (0) is less than n (5) or not
- The condition evaluates to be true, therefore program flow goes inside the loop and receives a number from 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 (supposing user enters 10 as first number). Therefore, sum=10
- Now program flow goes to updatation part of for loop and increments the value of i. Now i=1
- Checks the condition, because condition again evaluates to be true, therefore program flow again goes inside the loop and evaluates two statements
- That is, receives a number say 2 and stores it in arr[1]
- And sum+arr[1] or 10+2 gets initialized to sum. Now sum=12
- Continue the process until the value of i becomes 5 or the condition i<n evaluates to be 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, that is sum/size, that is sum/5. Put the value of sum. The variable armean holds its value (sum/size). Therefore just print the value of armean as output, that will be the arithmetic mean
Using Function
This function does the same job as of previous program. But it is created using function. That is, a function arithmeticMean() takes two arguments, the first one is the array, and second is the size. And returns the arithmetic mean of all numbers stored in the array. To lean more about function in C++, you can follow the separate tutorial on it.
#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 2 as size, and 10, 3 as two numbers:
Same Program in Other Languages
« Previous Program Next Program »