- C++ Programming Examples
- C++ Programming Examples
- C++ Print Hello World
- C++ Get Input from User
- C++ Print Integer
- C++ Add Two Numbers
- 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 equal Original
- C++ Add Subtract Multiply Divide
- C++ Make Calculator
- C++ Add Digits of Number
- C++ Find Average, Percentage
- C++ Calculate Arithmetic Mean
- C++ Calculate Student Grade
- C++ Print Multiplication Table
- C++ Print Prime Numbers
- C++ Add n Numbers
- C++ Interchange Digits of Number
- C++ Reverse a Number
- C++ Swap Two Numbers
- C++ Count Positive Negative Zero
- C++ Largest of Two Numbers
- C++ Largest of Three Numbers
- C++ Find Factorial of Number
- C++ Find HCF & LCM
- C++ Area, Perimeter of Square
- C++ Area, Perimeter of Rectangle
- C++ Area, Perimeter of Triangle
- C++ Area, Circumference of Circle
- C++ Convert Fahrenheit to Celsius
- C++ Convert Celsius to Fahrenheit
- C++ Print ASCII Value
- C++ Print Fibonacci Series
- C++ Check Palindrome or Not
- C++ Check Armstrong or Not
- C++ Generate Armstrong Numbers
- C++ Find nCr and nPr
- 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++ Print Diamond Pattern
- C++ Print Floyd's Triangle
- C++ Print Pascal's Triangle
- C++ Print Smiling Face
- C++ 1D Array Program
- C++ Linear Search
- C++ Binary Search
- C++ Add Two Numbers using Pointer
- C++ Find Largest Element in Array
- C++ Find Smallest Element in Array
- 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++ 2D Array Programs
- C++ Add Two Matrices
- C++ Subtract Two Matrices
- C++ Transpose Matrix
- C++ Multiply Two Matrices
- C++ 3D Array 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++ 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++ Generate Random Numbers
- C++ Read a File
- C++ Write Content to File
- C++ Read & Display File
- C++ Copy a File
- C++ Merge Two Files
- C++ List Files in Directory
- C++ Delete a File
- C++ Encrypt & Decrypt a File
- C++ Print Date/Time
- C++ Get IP Address
- C++ Shutdown, Restart Computer
- C++ More Programs
- C++ Frequency of Word
- C++ Programming Tutorial
- C++ Tutorial
- C++ Programming Test
- C++ Programming Test
- Give Online Test
- All Test List

« Previous Program Next Program »

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

If there are **n** set of numbers, therefore to find arithmetic mean, we have a formula:

am = (n_{1}+n_{2}+n_{3}+...+n_{n})/n

Here **am** indicates arithmetic mean, whereas **n _{1}**,

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.

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.

// C++ Program to Find Arithmetic Mean // ----codescracker.com #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

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.

// Find Arithmetic Mean using Function // ----codescracker.com #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:

« Previous Program Next Program »

© Copyright 2021. All Rights Reserved.