Programa para imprimir la serie Progresión Aritmética

Dado el primer término (a), la diferencia común (d) y un número entero n de la serie Progresión aritmética, la tarea es imprimir la serie. 
Ejemplos: 
 

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

Acercarse : 
 

Sabemos que la serie Progresión aritmética es como = 2, 5, 8, 11, 14…. … 
En esta serie 2 es el término declarante de la serie. 
Diferencia común = 5 – 2 = 3 (Diferencia común en la serie). 
entonces podemos escribir la serie como: 
t1 = a1 
t2 = a1 + (2-1) * d 
t3 = a1 + (3-1) * d 



tn = a1 + (n-1) * d 
 

CPP

// 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;
curr_term=a;
for (int i = 1; i <= n; i++)
{   cout << curr_term << " ";
    curr_term =curr_term + d;
     
}
}
 
// 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;
}

Java

// 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;
curr_term=a;
    for (int i = 1; i <= n; i++)
    { System.out.print(curr_term + " ");
    curr_term =curr_term + d;
    
    }
}
 
// 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.

Python3

# Python 3 Program to
# print an arithmetic
# progression series
def printAP(a,d,n):
 
      # Printing AP by simply adding d
      # to previous term.
      curr_term
curr_term=a
 
      for i in range(1,n+1):
print(curr_term, end=' ')
         curr_term =curr_term + d
          
 
# 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.

C#

// 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;
curr_term=a;
        for (int i = 1; i <= n; i++)
            {
Console.Write(curr_term + " ");            
curr_term += d;
 
              
            }
    }
 
    // 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.

PHP

<?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;
$curr_term=a;
    for ($i = 1; $i <= $n; $i++)
    {     echo($curr_term . " ");
        $curr_term += $d;
    
    }
}
 
// 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.
?>

Javascript

<script>
 
// JavaScript Program to print an arithmetic 
// progression series
   
    function printAP(a, d, n)
    {
   
        // Printing AP by simply adding d
        // to previous term.
        let curr_term;
        curr_term=a;
        for (let i = 1; i <= n; i++)
        {   document.write(curr_term + " ");
            curr_term =curr_term + d;
       
        }
    }
   
    // Driver code
 
    // starting number    
    let a = 2; 
       
    // Common difference
    let d = 1; 
       
    // N th term to be find
    let n = 5; 
   
    printAP(a, d, n);
   
// This code is contributed by Surbhi Tyagi
 
</script>
Producción: 

2 3 4 5 6

 

Complejidad de tiempo : O(n) donde n es el número total de términos de un AP dado

Espacio Auxiliar: O(1)

Publicación traducida automáticamente

Artículo escrito por nickhilrawat y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *