Problem statement:- Program to print series 1 2 5 8 15 28 51 94 ...N.
Data requirement:-
Input Data:- n
Output Data:-d
Additional Data:- i, a, b, c
Program in C
Output Data:-d
Additional Data:- i, a, b, c
Program in C
Here is the source code of the C Program to print series 1 2 5 8 15 28 51 94 ...N.
Code:
#include<stdio.h>
int main()
{
int n,i;
printf("Enter the range of number(Limit):");
scanf("%d",&n);
if(n >=1)
printf("1 ");
if(n>=2)
printf("2 ");
if(n >=3)
printf("5 ");
int a = 1;
int b = 2;
int c = 5;
int d;
for(i=4;i<=n;i++)
{
d = a+b+c;
a = b;
b = c;
c = d;
printf("%d ",d);
}
}
Enter the range of number(Limit):8
1 2 5 8 15 28 51 94
Program in C++
Here is the source code of the C++ Program to print series 1 2 5 8 15 28 51 94 ...N.
Code:
#include<iostream>
using namespace std;
int main()
{
int n,i;
cout<<"Enter the range of number(Limit):";
cin>>n;
if(n >=1)
cout<<1<<" ";
if(n>=2)
cout<<2<<" ";
if(n >=3)
cout<<5<<" ";
int a = 1;
int b = 2;
int c = 5;
int d;
for(i=4;i<=n;i++)
{
d = a+b+c;
a = b;
b = c;
c = d;
cout<<d<<" ";
}
}
Enter the range of number(Limit):5
1 2 5 8 15
Program in Java
Here is the source code of the Java Program to print series 1 2 5 8 15 28 51 94 ...N.
Code:
import java.util.Scanner;
public class Print_Series9 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n,i;
System.out.printf("Enter the range of number(Limit):");
n=sc.nextInt();
if(n >=1)
System.out.print("1 ");
if(n>=2)
System.out.print("2 ");
if(n >=3)
System.out.print("5 ");
int a = 1;
int b = 2;
int c = 5;
int d;
for(i=4;i<=n;i++)
{
d = a+b+c;
a = b;
b = c;
c = d;
System.out.print(d+" ");
}
sc.close();
}
}
Enter the range of number(Limit):6
1 2 5 8 15 28
Program in Python
Here is the source code of the Python Program to print series 1 2 5 8 15 28 51 94 ...N.
Code:
n=int(input("Enter the range of number(Limit):"))
i=4
if n>=1:
print("1 ",end="")
if n>=2:
print("2 ",end="")
if n>=3:
print("5 ",end="")
a=1
b=2
c=5
while i<=n:
d = a + b + c
a = b
b = c
c = d
print(d,end=" ")
i+=1
Enter the range of number(Limit):7
1 2 5 8 15 28 51
More Questions:-
0 Comments
Please do not Enter any spam link in the comment box