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

Posted in

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

Vinay Khatri
Last updated on November 23, 2024

    We all studied several sequences or series of mathematics in our childhood, such as arithmetic sequences, geometric sequences, harmonic series, Fibonacci series, etc. Isn’t printing those sequences or series on your computer systems using different programming languages interesting? This tutorial will help you write a program to print the given series: 1 2 4 8 16 32 64 128 in C, C++, Java, and Python programming languages.

    But before that, let us learn more about the given series and identify its type.

    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. Hence, it is a geometric progression or geometric sequence. We get this series by multiplying the previous number with a constant value of 2. In simple terms, the series forms by doubling the previous number.

    Now, we will write a script asking the user to enter a number n that will define the length of the series or the number of elements present in that sequence.

    Example

    Let's say the user enters 5 the first time and 9 the second time. The output series must have 5 and 9 numbers, respectively. Here are examples of the input and output combination.

    Input: n= 5

    Output: 1 2 4 8 16

    Input: n=9

    Output: 1 2 4 8 16 32 64 128 256

    Prerequisites

    Here are some prerequisites you need to write the above program:

    • Programming Language

    In this tutorial, we will help you learn to write the program in 4 languages – C, C++, Java, and Python.

    You must have a basic understanding of at least one of these programming languages to write the program. You must know the basic constructs of writing a program in that language.

    For instance, let us say you choose C. You must know the basic construct, such as header files, the main function, functions to take user input and display the output, etc. In addition, knowing the syntax of the language is essential. Without syntax, it is not possible to write a program.

    • Basic Understanding of Programming Concepts

    Having a good grasp of programming concepts, such as data types, variables, basic arithmetic operations, and conditional statements.

    • Knowledge of Loops

    Loops in computer programming execute a particular set of instructions or a block of code multiple times without the need to write code repetitively.

    In our tutorial, we used a while loop to print the given geometric series. A while loop iterates and executes the code as long as the specific condition holds true.

    Algorithm

    An Algorithm is a sequence of instructions to solve any programming problem. Writing an algorithm before implementing a program saves significant time.

    Here is an algorithm for our problem.

    1. Start
    2. Declare two integer variables, ‘n’ and ‘sequence’. Initialize the ‘sequence’ variable to 1.
    3. Ask the user to enter the length of the series to be printed.
    4. Read the user value using the scanf() function.
    5. Use a while loop with a condition (n!=0).
    6. Inside the loop, print the value of the ‘sequence’ variable and multiply it by 2.
    7. Store the result in the ‘sequence’ variable.
    8. Decrease n by 1.
    9. End the while loop.
    10. Return ‘0’ to indicate the end of the program and close the main function.
    11. Stop.

    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 the sequence
    printf("Enter the length of Series: ");
    scanf("%d", &n);
    
    while(n!=0)
    {
    printf("%d ", sequence);
    sequence*=2;
    n-=1;
    }
    return 0;
    }

    Output

    Enter the length of Series: 10
    1248163264128256512

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

    The logic will remain the same; you just need to implement it in C++.

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

    Output

    Enter the length of Series: 6
    12481632

    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;
    
    publicclassMain {
    publicstaticvoidmain(String args[]) {
    int sequence=1;
    
    Scanner sc = new Scanner(System.in);
    System.out.print("Enter the length of sequence: "); 
    
    //input the length 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
    1248163264128256

    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 this geometric series 1 2 4 8 16.... 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
    12481632641282565121024

    Complexity Analysis

    • Time Complexity: O(N), where N is the total length of the series. This is because we have run the while loop for the n number of times.
    • Space Complexity: O(1), constant space complexity. This is because we have used only one variable to store the value without any other data structures .

    Wrapping Up!

    This was all about writing a program to print the geometric progression in four different programming languages – C, C++, Java, and Python. In the programs, we asked the user to enter the length of the series and printed the series according to the entered value. Also, we used a while loop to iterate and print the series. You can even try this program using a for loop or do-while loop.

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

    Happy learning!

    People are also reading:

    Leave a Comment on this Post

    0 Comments