New: 203 pattern programs with tested code in C, C++, Java and Python Explore patterns

Doubling Star Triangle (1, 2, 4, 8, 16 Stars)

Read summary with AI tools

In the doubling star triangle each row has twice as many stars as the row above: 1, 2, 4, 8 and 16. Some tutorials call it the exponentially increasing star pattern.

Quick answer: Row i prints 2i − 1 stars. In code that is 1 << (i − 1): shifting 1 to the left by i − 1 bits doubles it that many times. Row 1 prints 1 star and row 5 prints 16.

This program is part of the Star Patterns collection.

Table of contents8 sections
  1. Doubling Star Triangle output
  2. How the doubling star triangle works
  3. Doubling Star Triangle in C
  4. Doubling Star Triangle in C++
  5. Doubling Star Triangle in Java
  6. Doubling Star Triangle in Python
  7. Time and space complexity
  8. Frequently asked questions

Doubling Star Triangle output

Output (n = 5):

*
**
****
********
****************
Doubling Star Triangle layout diagram, row by row (n = 5)
How each row of the doubling star triangle is built (n = 5). Blue cells are printed characters; grey dots are spaces.

How the doubling star triangle works

Most star triangles grow by a fixed amount per row. This one grows by a fixed factor, so the row length is a power of 2. The left shift operator gives powers of 2 without any multiplication loop or pow() call: 1 << k is 2k. It also shows how quickly doubling grows: ten rows would need 512 stars on the last line.

  1. Run the outer loop for i from 1 to n, one row per pass.
  2. Print (1 << (i − 1)) stars.
  3. Move to the next line.

Key takeaways

  • Row i has 2i − 1 stars.
  • 1 << k equals 2k and works the same in C, C++, Java and Python.
  • The total number of stars for n rows is 2n − 1: 31 for five rows.
  • Doubling grows so fast that a few more rows no longer fit on one screen.

Doubling Star Triangle in C

#include <stdio.h>

int main(void) {
    int n = 5;

    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= (1 << (i - 1)); j++)
            printf("*");
        printf("\n");
    }
    return 0;
}

Doubling Star Triangle in C++

#include <iostream>
using namespace std;

int main() {
    int n = 5;

    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= (1 << (i - 1)); j++)
            cout << "*";
        cout << endl;
    }
    return 0;
}

Doubling Star Triangle in Java

public class DoublingStarTriangle {
    public static void main(String[] args) {
        int n = 5;

        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= (1 << (i - 1)); j++)
                System.out.print("*");
            System.out.println();
        }
    }
}

Doubling Star Triangle in Python

n = 5

for i in range(1, n + 1):
    for j in range(1, (1 << (i - 1)) + 1):
        print("*", end="")
    print()

Time and space complexity

The program prints 2n − 1 stars, so the running time grows as O(2n). The extra memory is O(1).

Frequently asked questions

Why use 1 << (i − 1) instead of pow(2, i − 1)?

pow works with floating-point numbers and needs <math.h> in C. The shift stays in whole numbers and is exact, which is what a loop count needs.

How many stars are printed in total?

1 + 2 + 4 + … + 2n − 1 = 2n − 1. For five rows that is 31 stars, one less than the next power of 2.

Can I make it grow by a factor of 3?

Yes. Keep a variable count that starts at 1 and multiply it by 3 after each row. The rows then have 1, 3, 9, 27 stars.

Authored by

Ghanendra Yadav runs Pattern Programs from Bangalore, India. The site started in 2020 to help students practise loops and logic through pattern programs in C, C++, Java and Python. Every program published here is compiled and run before it goes live, and the output shown is the real output.

Latest articles

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.