Find N Arithmetic Means between A and B - GeeksforGeeks

04 May, 2022
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 
20 22 24 26 28 30 32 

Input : A = 5  B = 35  N = 5
Output : 10 15 20 25 30
Approach : Let A1, A2, A3, A4......An be N Arithmetic Means between two given numbers A and B . Then A, A1, A2 ..... An, 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++
Java
Python3
C#
PHP
Javascript
// 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 aboveint main(){    int A = 20, B = 32, N = 5;    printAMeans(A, B, N);       return 0;}
// java program to illustrate// n arithmetic mean between// A and Bimport 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);    }}
# 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 codeA = 20; B = 32; N = 5printAMeans(A, B, N) # This code is contributed by Smitha Dinesh Semwal
// C# program to illustrate// n arithmetic mean between // A and Busing 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
<?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.?>
<script> // JavaScript 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)    let d = (B - A) / (N + 1)         // For finding N the arithmetic    // mean between A and B    for(let i = 1; i < N + 1; i++)        document.write(Math.floor(A + i * d)," ")} // Driver codelet A = 20, B = 32, N = 5;printAMeans(A, B, N) // This code is contributed by Shinjanpatra </script>
22 24 26 28 30
vt_m
ManasChhabra2
shinjanpatra
arithmetic progression
series
statistical-algorithms
Mathematical
Mathematical
series
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Modulo Operator (%) in C/C++ with Examples
Print all possible combinations of r elements in a given array of size n
Segment Tree | Set 1 (Sum of given range)
Prime Numbers
Merge two sorted arrays
The Knight's tour problem | Backtracking-1
Program to find GCD or HCF of two numbers
Product of Array except itself
Write a program to reverse digits of a number
Find minimum number of coins that make a given value