Problem statement:- Program to print series 1,3,7,15,31...n.
Example:
Input: n=5
Output: 1 3 7 15 31 /* 0*2+1 1*2+1 3*2+1 7*2+1 15*2+1*/
Input: n=7
Output: 1 3 7 15 31 63 127 /* 0*2+1 1*2+1 3*2+1 7*2+1 15*2+1 31*2+1 63*2+1*/
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 1,3,7,15,31...n.
Code:
#include<stdio.h>
int main()
{
int n,i,pr=0;
printf("Enter the range of number(Limit):");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
pr=(pr*2)+1;
printf("%d ",pr);
}
}
Enter the range of number(Limit):5
1 3 7 15 31
Program in C++
Here is the source code of the C++ Program to print series 1,3,7,15,31...n.
Code:
#include<iostream>
using namespace std;
int main()
{
int n,i,pr=0;
cout<<"Enter the range of number(Limit):";
cin>>n;
for(i=1;i<=n;i++)
{
pr=(pr*2)+1;
cout<<pr<<" ";
}
}
Enter the range of number(Limit):7
1 3 7 15 31 63 127
Program in Java
Here is the source code of the Java Program to print series 1,3,7,15,31...n.
Code:
import java.util.Scanner;
public class Print_Series2 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n,i,pr=0;
System.out.printf("Enter the range of number(Limit):");
n=sc.nextInt();
for(i=1;i<=n;i++)
{
pr=(pr*2)+1;
System.out.print(pr+" ");
}
sc.close();
}
}
Enter the range of number(Limit):6
1 3 7 15 31 63
Program in Python
Here is the source code of the Python Program to print series 1,3,7,15,31...n.
Code:
n=int(input("Enter the range of number(Limit):"))
i=1
pr=0
while i<=n:
pr = (pr * 2) + 1
print(pr,end=" ")
i+=1
Enter the range of number(Limit):4
1 3 7 15
More Questions:-
0 Comments
Please do not Enter any spam link in the comment box