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
Doubling Star Triangle output
Output (n = 5):
* ** **** ******** ****************

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.
- Run the outer loop for
ifrom1ton, one row per pass. - Print
(1 << (i − 1))stars. - Move to the next line.
Key takeaways
- Row
ihas 2i − 1 stars. 1 << kequals 2k and works the same in C, C++, Java and Python.- The total number of stars for
nrows 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.











