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
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);
}
}
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<<" ";
}
}
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();
}
}
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
Enter the range of number(Limit):5
2 4 7 12 21
More Questions:-
0 Comments
Please do not Enter any spam link in the comment box