Write a Program to Print the Given Series:1 2 4 8 16 32 64 128

By | November 17, 2021
Write a Program to print given series 1, 2, 4, 8, 16, 32, 64, 128

In this programming tutorial, we will discuss a program to print the given series: 1 2 4 8 16 32 64 128 in C, C++, Java, and Python programming languages.

The Given Series

1 2 4 8 16 32 64 128 256.........

As you can see that the given sequence starts from 1, and every subsequent number is twice the previous number. This is a geometric sequence. Now we will write a script that will ask the user to enter a number n that will define the length of series or number of elements present in that sequence.

Example 1

Let’s say the user enters 5 the first time and 9 the second time. Then the output series must have 5 and 9 numbers, respectively.

Input n= 5

Output 1 2 4 8 16

Example 2

Input n=9
Output 1 2 4 8 16 32 64 128 256

C Program to Print the Given Series: 1 2 4 8 16 32 64 128

Let’s begin with implementing the program that can print 1 2 4 8 16…. series in C.

#include <stdio.h>

int main() 
{
   int n, sequence=1;
   
   //enter the length of sequence
   printf("Enter the length of Series: ");
   scanf("%d", &n);
   
   while(n!=0)
   {
   	printf("%d ", sequence);
   	sequence*=2;
   	n-=1;
   }
   return 0;
}

Output

Vamware
Enter the length of Series: 10
1 2 4 8 16 32 64 128 256 512

C++ Program to Print the Given Series:1 2 4 8 16 32 64 128

The logic will remain the same for the C++ program.

#include <iostream>
using namespace std;
 
int main() 
{	
   int n, sequence =1;
   //enter the length of sequence
   cout<<"Enter the length of Series: "; cin>>n;
   
   while(n!=0)
   {
   	cout<<sequence<<" ";
   	sequence*=2;
   	n-=1;
   }
   return 0;
}

Output

Vamware
Enter the length of Series: 6
1 2 4 8 16 32

Java Program to Print the Given Series: 1 2 4 8 16 32 64 128

Now let’s write a program to print the series 1 2 4 8 16….. till n terms in Java.

import java.util.Scanner;

public class Main {
    public static void main(String args[]) {
        int sequence=1;
        
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter the length of sequence: ");      

        // input the lenght of the sequence
        int n = sc.nextInt();
        while(n!=0)
        {
           System.out.print(sequence);
           System.out.print(" ");
           sequence*=2;
           n-=1;
        }
    }
}

Output

Enter the length of sequence: 9
1 2 4 8 16 32 64 128 256

Python Program to Print the Given Series: 1 2 4 8 16 32 64 128

Python syntax is very simple as compared to other programming languages. And implementing the print series 1 2 4 8 16…. till the nth term is also straightforward.

# enter the length of sequence
n = int(input("Enter the length of Series: "))

sequence=1

while(n):
    print(sequence, end =" ")
    sequence*=2
    n-=1

Output

Enter the length of Series: 11
1 2 4 8 16 32 64 128 256 512 1024

Complexity Analysis

  • Time Complexity: O(N), where N is the total length of the series.
  • Space Complexity: O(1), constant space complexity.

Wrapping Up!

In this tutorial, we learned how to print the 1 2 4 8 16 32 64 128...... sequence in four different programming languages, namely C, C++, Java, and Python. In the program, we asked the user to enter the length of the series and print the series according to the user-entered value.

If you like this article or have any suggestions, please let us know by commenting down below. Happy learning!

Vamware

People are also reading:

Leave a Reply

Your email address will not be published.