Program to Print series 1,2,8,16,32...n

Problem statement:- Program to print series 1,2,8,16,32...n.

Example:    

                Input: n=35

                Output: 1 2 4 8 16 32 /* 1  1*2  2*2  4*2  8*2  16*2 */

                Input: n=9

                Output: 1 2 4 8 /* 1  1*2  2*2  4*2 */


 Data requirement:-

   Input Data:- n

  Output Data:-i

Program in C
  
Here is the source code of the C Program to print series 1,2,8,16,32...n.

Code:

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

Input/Output:
Enter the range of number(Limit):35
1 2 4 8 16 32

Program in C++

Here is the source code of the C++ Program to print series 1,2,8,16,32...n.

Code:

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

Input/Output:
Enter the range of number(Limit):9
1 2 4 8

Program in Java

Here is the source code of the Java Program to print series 1,2,8,16,32...n.

Code:

import java.util.Scanner;
public class Print_Series {

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();
    for(i=1;i<=n;i*=2)
    {
    System.out.print(i+" ");
    }
    sc.close();
}
}

Input/Output:
Enter the range of number(Limit):70
1 2 4 8 16 32 64 

Program in Python

Here is the source code of the Python Program to print series 1,2,8,16,32...n.

Code:

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

Input/Output:
Enter the range of number(Limit):100
1 2 4 8 16 32 64


Post a Comment

0 Comments