Given three integers A, B and N the task is to find N Arithmetic means between A and B. We basically need to insert N terms in an Arithmetic progression. where A and B are first and last terms.

Examples:

Input : A = 20 B = 32 N = 5 Output : 22 24 26 28 30 The Arithmetic progression series as 2022 24 26 28 3032 Input : A = 5 B = 35 N = 5 Output : 10 15 20 25 30

**Approach :**

Let A_{1}, A_{2}, A_{3}, A_{4}……A_{n} be N Arithmetic Means between two given numbers A and B . Then A, A_{1}, A_{2} ….. A_{n}, B will be in Arithmetic Progression .

Now B = (N+2)^{th} term of the Arithmetic progression .

So :

Finding the (N+2)^{th} term of the Arithmetic progression Series

where d is the Common Difference

B = A + (N + 2 – 1)d

B – A = (N + 1)d

So the Common Difference d is given by.

**d = (B – A) / (N + 1)**

So now we have the value of** A **and the value of the **common difference(d)**,

now we can find all the N Arithmetic Means between A and B.

## C++

`// C++ program to find n arithmetic ` `// means between A and B ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Prints N arithmetic means between ` `// A and B. ` `void` `printAMeans(` `int` `A, ` `int` `B, ` `int` `N) ` `{ ` ` ` `// calculate common difference(d) ` ` ` `float` `d = (` `float` `)(B - A) / (N + 1); ` ` ` ` ` `// for finding N the arithmetic ` ` ` `// mean between A and B ` ` ` `for` `(` `int` `i = 1; i <= N; i++) ` ` ` `cout << (A + i * d) <<` `" "` `; ` `} ` ` ` `// Driver code to test above ` `int` `main() ` `{ ` ` ` `int` `A = 20, B = 32, N = 5; ` ` ` `printAMeans(A, B, N); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// java program to illustrate ` `// n arithmetic mean between ` `// A and B ` `import` `java.io.*; ` `import` `java.lang.*; ` `import` `java.util.*; ` ` ` `public` `class` `GFG { ` ` ` ` ` `// insert function for calculating the means ` ` ` `static` `void` `printAMeans(` `int` `A, ` `int` `B, ` `int` `N) ` ` ` `{ ` ` ` `// Finding the value of d Common difference ` ` ` `float` `d = (` `float` `)(B - A) / (N + ` `1` `); ` ` ` ` ` `// for finding N the Arithmetic ` ` ` `// mean between A and B ` ` ` `for` `(` `int` `i = ` `1` `; i <= N; i++) ` ` ` `System.out.print((A + i * d) + ` `" "` `); ` ` ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main(String args[]) ` ` ` `{ ` ` ` `int` `A = ` `20` `, B = ` `32` `, N = ` `5` `; ` ` ` `printAMeans(A, B, N); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to find n arithmetic ` `# means between A and B ` ` ` `# Prints N arithmetic means ` `# between A and B. ` `def` `printAMeans(A, B, N): ` ` ` ` ` `# Calculate common difference(d) ` ` ` `d ` `=` `(B ` `-` `A) ` `/` `(N ` `+` `1` `) ` ` ` ` ` `# For finding N the arithmetic ` ` ` `# mean between A and B ` ` ` `for` `i ` `in` `range` `(` `1` `, N ` `+` `1` `): ` ` ` `print` `(` `int` `(A ` `+` `i ` `*` `d), end ` `=` `" "` `) ` ` ` `# Driver code ` `A ` `=` `20` `; B ` `=` `32` `; N ` `=` `5` `printAMeans(A, B, N) ` ` ` `# This code is contributed by Smitha Dinesh Semwal ` |

*chevron_right*

*filter_none*

## C#

`// C# program to illustrate ` `// n arithmetic mean between ` `// A and B ` `using System; ` ` ` `public class GFG { ` ` ` ` ` `// insert function for calculating the means ` ` ` `static void printAMeans(int A, int B, int N) ` ` ` `{ ` ` ` `// Finding the value of d Common difference ` ` ` `float d = (float)(B - A) / (N + 1); ` ` ` ` ` `// for finding N the Arithmetic ` ` ` `// mean between A and B ` ` ` `for (int i = 1; i <= N; i++) ` ` ` `Console.Write((A + i * d) + " "); ` ` ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public static void Main() ` ` ` `{ ` ` ` `int A = 20, B = 32, N = 5; ` ` ` `printAMeans(A, B, N); ` ` ` `} ` `} ` `// Contributed by vt_m ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to find n arithmetic ` `// means between A and B ` ` ` `// Prints N arithmetic means ` `// between A and B. ` `function` `printAMeans(` `$A` `, ` `$B` `, ` `$N` `) ` `{ ` ` ` ` ` `// calculate common ` ` ` `// difference(d) ` ` ` `$d` `= (` `$B` `- ` `$A` `) / (` `$N` `+ 1); ` ` ` ` ` `// for finding N the arithmetic ` ` ` `// mean between A and B ` ` ` `for` `(` `$i` `= 1; ` `$i` `<= ` `$N` `; ` `$i` `++) ` ` ` `echo` `(` `$A` `+ ` `$i` `* ` `$d` `) ,` `" "` `; ` `} ` ` ` ` ` `// Driver Code ` ` ` `$A` `= 20; ` `$B` `= 32; ` ` ` `$N` `= 5; ` ` ` `printAMeans(` `$A` `, ` `$B` `, ` `$N` `); ` ` ` `// This code is Contributed by vt_m. ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

22 24 26 28 30

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Find N Geometric Means between A and B
- Find mean of subarray means in a given array
- Find Harmonic mean using Arithmetic mean and Geometric mean
- Find an integer that is common in the maximum number of given arithmetic progressions
- Find the missing number in unordered Arithmetic Progression
- Find the missing number in Arithmetic Progression
- Implement *, - and / operations using only + arithmetic operator
- Longest string in non-decreasing order of ASCII code and in arithmetic progression
- Ratio of mth and nth term in an Arithmetic Progression (AP)
- Arithmetic Progression containing X and Y with least possible first term
- Arithmetic Sequences - Sequences and Series | Class 11 Maths
- Add two numbers without using arithmetic operators
- Longest Arithmetic Progression | DP-35
- Modular Exponentiation (Power in Modular Arithmetic)
- How to sum two integers without using arithmetic operators in C/C++?
- Check whether Arithmetic Progression can be formed from the given array
- Count of AP (Arithmetic Progression) Subsequences in an array
- Decimal to binary conversion without using arithmetic operators
- Program for sum of arithmetic series
- Minimum De-arrangements present in array of AP (Arithmetic Progression)

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.