Program to print Arithmetic Progression series

Given first term (a), common difference (d) and a integer n of the Arithmetic Progression series, the task is to print the series.

Examples :

Input : a = 5, d = 2, n = 10
Output : 5 7 9 11 13 15 17 19 21 23

Approach :

We know the Arithmetic Progression series is like = 2, 5, 8, 11, 14 …. …
In this series 2 is the stating term of the series .
Common difference = 5 – 2 = 3 (Difference common in the series).
so we can write the series as :
t1 = a1
t2 = a1 + (2-1) * d
t3 = a1 + (3-1) * d
.
.
.
tn = a1 + (n-1) * d

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP Program to print an arithmetic 
// progression series
#include <bits/stdc++.h>
using namespace std;
  
void printAP(int a, int d, int n)
{
  
// Printing AP by simply adding d
// to previous term.
int curr_term = 0;
for (int i = 1; i <= n; i++)
{
    curr_term += d;
    cout << curr_term << " ";
}
}
  
// Driver code
int main() 
{
    // starting number    
    int a = 2; 
      
    // Common difference
    int d = 1; 
      
    // N th term to be find
    int n = 5; 
  
    printAP(a, d, n);
  
    return 0;
}
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program to print an arithmetic
// progression series
class GFG 
{
static void printAP(int a, int d, int n) 
{
  
    // Printing AP by simply adding d
    // to previous term.
    int curr_term = 0;
    for (int i = 1; i <= n; i++) 
    {
    curr_term += d;
    System.out.print(curr_term + " ");
    }
}
  
// Driver code
public static void main(String[] args) 
{
// starting number
int a = 2
  
// Common difference
int d = 1
  
// N th term to be find
int n = 5
  
printAP(a, d, n);
}
}
// This code is contributed by Anant Agarwal.
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

# Pyhon 3 Program to
# print an arithmetic 
# progression series
def printAP(a,d,n):
  
      # Printing AP by simply adding d
      # to previous term.
      curr_term = 0
  
      for i in range(1,n+1):
         curr_term += d
         print(curr_term, end=' ')
  
# Driver code
a = 2    # starting number
d = 1    # Common difference
n = 5    # N th term to be find
  
printAP(a, d, n)
  
# This code is contributed 
# by Azkia Anam.
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# Program to print an arithmetic
// progression series
using System;
  
class GFG 
{
    static void printAP(int a, int d, int n) 
    {
        // Printing AP by simply adding
        // d to previous term.
        int curr_term = 0;
        for (int i = 1; i <= n; i++) 
            
             curr_term += d;
  
             Console.Write(curr_term + " ");
            }
    }
  
    // Driver code
    public static void Main() 
    {
        // starting number
        int a = 2; 
          
        // Common difference
        int d = 1; 
          
        // N th term to be find
        int n = 5; 
  
        printAP(a, d, n);
    }
}
// This code is contributed by vgt_m.
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP Program to print an arithmetic 
// progression series
  
function printAP($a, $d, $n)
{
  
    // Printing AP by simply adding d
    // to previous term.
    $curr_term = 0;
    for ($i = 1; $i <= $n; $i++) 
    {
        $curr_term += $d;
        echo($curr_term . " ");
    }
}
  
// Driver code
  
// starting number
$a = 2; 
  
// Common difference
$d = 1; 
  
// N th term to be find
$n = 5; 
  
printAP($a, $d, $n);
  
// This code is contributed by Ajit.
?>
chevron_right

Output:
1 2 3 4 5



Check out this Author's contributed articles.

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.



Improved By : jit_t



Article Tags :
Practice Tags :