Given a number N, the task is to print the star pattern in single loop.
Examples:
Input: N = 9
Output:
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * *
* * * * * * * * *
Input: N = 5
Output:
*
* *
* * *
* * * *
* * * * *
Please Refer article for printing the pattern in two loops as:
Triangle pattern in Java
Approach: The idea is to break a column into three parts and solve each part independently of others.
- Case 1: Spaces before the first *, which takes care of printing white spaces.
- Case 2: Starting of the first * and the ending of the last * in the row, which takes care of printing alternating white spaces and *.
- Case 3: The ending star essentially tells to print a new line or end the program if we have already finished n rows.
Refer the image below

Below is the implementation of above approach:
C++
// C++ implementation of printing // star pattern in single loop #include <iostream> using namespace std; // Function to print the star // pattern in single loop void pattern(int n) { int i, k, flag = 1; // Loop to handle number of rows and // columns in this case for (i = 1, k = 0; i <= 2 * n - 1; i++) { // Handles case 1 if (i < n - k) cout << " "; // Handles case 2 else { if (flag) cout << "*"; else cout << " "; flag = 1 - flag; } // Condition to check case 3 if (i == n + k) { k++; cout << endl; // Since for nth row we have // 2 * n- 1 columns if (i == 2 * n - 1) break; // Reinitializing i as 0, // for next row i = 0; flag = 1; } } } // Driver Code int main() { int n = 6; // Function Call pattern(n); return 0; } |
chevron_right
filter_none
Java
// Java implementation of printing // star pattern in single loop import java.util.*; class GFG{ // Function to print the star // pattern in single loop static void pattern(int n) { int i, k, flag = 1; // Loop to handle number of rows and // columns in this case for(i = 1, k = 0; i <= 2 * n - 1; i++) { // Handles case 1 if (i < n - k) System.out.print(" "); // Handles case 2 else { if (flag == 1) System.out.print("*"); else System.out.print(" "); flag = 1 - flag; } // Condition to check case 3 if (i == n + k) { k++; System.out.println(); // Since for nth row we have // 2 * n- 1 columns if (i == 2 * n - 1) break; // Reinitializing i as 0, // for next row i = 0; flag = 1; } } } // Driver code public static void main(String[] args) { int n = 6; // Function Call pattern(n); } } // This code is contributed by offbeat |
chevron_right
filter_none
C#
// C# implementation of printing // star pattern in single loop using System; class GFG{ // Function to print the star // pattern in single loop static void pattern(int n) { int i, k, flag = 1; // Loop to handle number of rows and // columns in this case for(i = 1, k = 0; i <= 2 * n - 1; i++) { // Handles case 1 if (i < n - k) Console.Write(" "); // Handles case 2 else { if (flag == 1) Console.Write("*"); else Console.Write(" "); flag = 1 - flag; } // Condition to check case 3 if (i == n + k) { k++; Console.WriteLine(); // Since for nth row we have // 2 * n- 1 columns if (i == 2 * n - 1) break; // Reinitializing i as 0, // for next row i = 0; flag = 1; } } } // Driver code public static void Main() { int n = 6; // Function call pattern(n); } } // This code is contributed by sanjoy_62 |
chevron_right
filter_none
Output
*
* *
* * *
* * * *
* * * * *
* * * * * *
Recommended Posts:
- Printing the Triangle Pattern using last term N
- Printing Triangle Pattern in Java
- Print pattern using only one loop | Set 1 (Using setw)
- Print the pattern by using one loop | Set 2 (Using Continue Statement)
- Program to print number with star pattern
- Program for Arrow Star Pattern
- Python | Print an Inverted Star Pattern
- Program to print half diamond Number-Star pattern
- Program to print Hut Star pattern
- Print the Alphabets A to Z in Star Pattern
- Program to print half Diamond star pattern
- Printing Heart Pattern in C
- Pattern Printing question asked in CGI Coding Round
- Inner reducing pattern printing
- Printing string in plus ‘+’ pattern in the matrix
- Program for Expressionless Face Pattern printing
- How to learn Pattern printing easily?
- Print a pattern without using any loop
- Sum of upper triangle and lower triangle
- Program to print a Hollow Triangle inside a Triangle
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

