- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Find N Arithmetic Means between A and B using C++.

Suppose we have three integers A, B and N. We have to find N arithmetic means between A and B. If A = 20, B = 32, and N = 5, then the output will be 22, 24, 26, 28, 30

The task is simple we have to insert N number of elements in the Arithmetic Progression where A and B are the first and last term of that sequence. Suppose A1, A2, …. An are n arithmetic means. So the sequence will be A, A1, A2, …. An, B. So B is the (N + 2)th term of the sequence. So we can use these formulae −

$$B=A+\lgroup N+2-1\rgroup*d$$

$$B-A=\lgroup N+2-1\rgroup*d$$

$$d=\frac{B-A}{\lgroup N+2-1\rgroup}$$

## Example

#include<iostream> using namespace std; void showMeans(int A, int B, int N) { float d = (float)(B - A) / (N + 1); for (int i = 1; i <= N; i++) cout << (A + i * d) <<" "; } int main() { int A = 20, B = 40, N = 5; showMeans(A, B, N); }

## Output

23.3333 26.6667 30 33.3333 36.6667

- Related Articles
- Find N Geometric Means between A and B using C++.
- Program to find greater value between a^n and b^n in C++
- Find the resistance between A and B."\n
- Find Harmonic mean using Arithmetic mean and Geometric mean using C++.
- If $n(A)=30, n(B) = 45, n(A cup B) = 65$ then find $n(A cap B)$, $n(A-B)$ and $n(B-A)$.
- Find the distance between the following pair of points:$(a + b, b + c)$ and $(a – b, c – b)$
- Find value of a and b."\n
- Find the only repetitive element between 1 to n-1 using C++
- Missing Number In Arithmetic Progression using C++
- Arithmetic of Number Systems\n
- Find mean of subarray means in a given array in C++
- What is the difference between K-Means and DBSCAN?
- Program to find N-th term of series a, b, b, c, c, c…in C++
- C Program for N-th term of Arithmetic Progression series
- Find the sum of the following arithmetic progressions:( a+b, a-b, a-3 b, ldots ) to 22 terms

Advertisements