Program to print series 2 4 7 12 21 ...N

 Problem statement:- Program to print series 2 4 7 12 21 38 71 ...N.

 Data requirement:-

   Input Data:- n

  Output Data:-pr

  Additional Data:- i

Program in C
  
Here is the source code of the C Program to print series 2 4 7 12 21 38 71...n.

Code:

#include<stdio.h>
int main()
{
    int n,i,pr=2;
    printf("Enter the range of number(Limit):");
    scanf("%d",&n);
    printf("2 ");
    for(i=0;i<n-1;i++)
    {
        pr=(pr*2)-i;
        printf("%d ",pr);
    }
}

Input/Output:
Enter the range of number(Limit):7
2 4 7 12 21 38 71

Program in C++

Here is the source code of the C++ Program to print series 2 4 7 12 21 38 71...n.

Code:

#include<iostream>
using namespace std;
int main()
{
    int n,i,pr=2;
    cout<<"Enter the range of number(Limit):";
    cin>>n;
    cout<<2<<" ";
    for(i=0;i<n-1;i++)
    {
        pr=(pr*2)-i;
        cout<<pr<<" ";
    }
}

Input/Output:
Enter the range of number(Limit):5
2 4 7 12 21

Program in Java

Here is the source code of the Java Program to print series 2 4 7 12 21 38 71...n.

Code:

import java.util.Scanner;
public class Print_Series12 {

public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n,i,pr=2;
   System.out.printf("Enter the range of number(Limit):");
    n=sc.nextInt();
    System.out.print("2 ");
    for(i=0;i<n-1;i++)
    {
        pr=(pr*2)-i;
    System.out.print(pr+" ");
    }
    sc.close();
}
}

Input/Output:
Enter the range of number(Limit):6
2 4 7 12 21 38 

Program in Python

Here is the source code of the Python Program to print series 2 4 7 12 21 38 71...n.

Code:

n=int(input("Enter the range of number(Limit):"))
i=0
pr=2
print("2 ",end="")
while i<n-1:
    pr = (pr * 2) -i
    print(pr,end=" ")
    i+=1

Input/Output:
Enter the range of number(Limit):5
2 4 7 12 21 


Post a Comment

0 Comments