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.
 Start
 Declare two integer variables, ‘n’ and ‘sequence’. Initialize the ‘sequence’ variable to 1.
 Ask the user to enter the length of the series to be printed.
 Read the user value using the scanf() function.
 Use a while loop with a condition (n!=0).
 Inside the loop, print the value of the ‘sequence’ variable and multiply it by 2.
 Store the result in the ‘sequence’ variable.
 Decrease n by 1.
 End the while loop.
 Return ‘0’ to indicate the end of the program and close the main function.
 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 dowhile loop.
If you like this article or have any suggestions, please let us know by commenting below.
Happy learning!
People are also reading:
 Find the root of quadratic equation
 Linear Search in Python and C++
 Check whether the given alphabet is a vowel or Not
 Find The Sum of Elements Above and Below The Main Diagonal of a Matrix
 Reverse a String
 Print the Equivalent name of the day on the basis of user input
 Union of Two Arrays
 Transpose a Matrix
 Find the sum of the series x+ x2/2 +x3/3 + ……. +xn/n
 Find the Sum of Series 1+x+x^2+……+x^n
Leave a Comment on this Post