0% found this document useful (0 votes)
141 views20 pages

Star Pattern in C++ (15 Programs With Output)

The document provides a tutorial on creating various star patterns in C++ through 15 different programs. Each program demonstrates the use of loops to generate patterns such as triangles, pyramids, diamonds, and hollow shapes, enhancing coding skills and logic. The document includes code snippets, explanations, and sample outputs for each pattern.

Uploaded by

narotamjakhar5
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
141 views20 pages

Star Pattern in C++ (15 Programs With Output)

The document provides a tutorial on creating various star patterns in C++ through 15 different programs. Each program demonstrates the use of loops to generate patterns such as triangles, pyramids, diamonds, and hollow shapes, enhancing coding skills and logic. The document includes code snippets, explanations, and sample outputs for each pattern.

Uploaded by

narotamjakhar5
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

6/22/25, 12:02 PM Star Pattern in C++ (15 Programs With Output)

Home Resources C++ Tutorial | Learn CPP Programming Language Program Star Pattern In C++ (1...

Share

Star Pattern in C++ (15 Programs With Output)


Star patterns in C++ make coding fun and help you learn loops easily.
These patterns use stars(*) to create cool shapes, improving your logic
and problem-solving skills. They start simple and get more creative as you
practice.
Whether you are just starting or already know coding, star pattern
programs in C++ are a fun way to practice and improve your skills. They
make learning loops and logic more interesting and enjoyable.

English
Coders ki Tipaniya:
Spaces aur stars ka ho sahi
distribution, tabhi hoga perfect
pattern execution!

Option 1: Simple Right-Angled Triangle Star Pattern


in C++
Code:

#include <iostream>
using namespace std;

https://www.wscubetech.com/resources/cpp/programs/star-pattern 1/20
6/22/25, 12:02 PM Star Pattern in C++ (15 Programs With Output)

int main() {
int rows;
cout << "Rows for pattern: "; // Taking the input from use
cin >> rows;

for (int i = 1; i <= rows; i++) { // Loop for rows


for (int j = 1; j <= i; j++) { // Loop for stars
cout << "*";
}
cout << endl; // Move to the next line
}
return 0;
}

Output:

Rows for pattern: 5


*
**
***
****
*****

=== Code Execution Successful ===

Code explanation:
We take a number from the user to decide how many rows the pattern
will have.
We use a loop to go through each row and another loop inside it to
print stars (*).
The number of stars in each row increases as we move to the next
row. After printing stars for a row, we move to the next line to create
the pattern.

Option 2: Inverted Right-Angled TriangleStar


Pattern in C++
Code:

#include <iostream>
using namespace std;

https://www.wscubetech.com/resources/cpp/programs/star-pattern 2/20
6/22/25, 12:02 PM Star Pattern in C++ (15 Programs With Output)

int main() {
int rows;
cout << "Rows for pattern: "; // Taking the input from use
cin >> rows;

for (int i = rows; i >= 1; i--) { // Loop for rows (decrea


for (int j = 1; j <= i; j++) { // Loop for stars
cout << "*";
}
cout << endl;
}
return 0;
}

Output:

Rows for pattern: 6


******
*****
****
***
**
*

=== Code Execution Successful ===

Code explanation:
First, we take a number from the user to decide how many rows the
pattern is.
We use the first loop to go through each row and another loop inside it
to print stars (*), decreasing the number of stars in each row.
After printing stars for a row, we move to the next line, creating an
inverted star pattern in C++.

Option 3: Pyramid Star Pattern in C++


Code:

#include <iostream>
using namespace std;

int main() {
int rows;
cout << "Rows for pattern: ";
https://www.wscubetech.com/resources/cpp/programs/star-pattern 3/20
6/22/25, 12:02 PM Star Pattern in C++ (15 Programs With Output)

cin >> rows;

for (int i = 1; i <= rows; i++) {


for (int j = 1; j <= rows - i; j++) // Printing spaces
cout << " ";
for (int j = 1; j <= 2 * i - 1; j++) // Printing stars
cout << "*";
cout << endl;
}
return 0;
}

Output:

Rows for pattern: 5


*
***
*****
*******
*********

=== Code Execution Successful ===

Code Explanation:
We take a number from the user to decide how many rows the pattern
will have.
A loop creates each row, adding spaces first to align the stars (*) in a
pyramid shape.
The number of stars increases in each row, forming a star pattern in
C++ that looks like a pyramid.

Option 4: Inverted Pyramid Star Pattern in C++


Code:

#include <iostream>
using namespace std;

int main() {
int rows;
cout << "Rows for pattern: ";
cin >> rows;

for (int i = rows; i >= 1; i--) {


https://www.wscubetech.com/resources/cpp/programs/star-pattern 4/20
6/22/25, 12:02 PM Star Pattern in C++ (15 Programs With Output)

for (int j = 1; j <= rows - i; j++) // Printing spaces


cout << " ";
for (int j = 1; j <= 2 * i - 1; j++) // Printing stars
cout << "*";
cout << endl;
}
return 0;
}

Output:

Rows for pattern: 5


*********
*******
*****
***
*

=== Code Execution Successful ===

Code explanation:
We first take the number of rows as input from the user to decide the
size of the pattern.
A loop creates each row, adding spaces first to align the stars (*) in an
inverted pyramid shape.
The number of stars decreases in each row, forming an inverted star
pattern in a C++ program that looks like an upside-down pyramid.

Option 5: Diamond Star Pattern in C++


Code:

#include <iostream>
using namespace std;

int main() {
int rows;
cout << "Rows for pattern: "; // Taking the input form use
cin >> rows;

// Upper part of the diamond


for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= rows - i; j++)
cout << " ";
https://www.wscubetech.com/resources/cpp/programs/star-pattern 5/20
6/22/25, 12:02 PM Star Pattern in C++ (15 Programs With Output)

for (int j = 1; j <= 2 * i - 1; j++)


cout << "*";
cout << endl;
}

// Lower part of the diamond


for (int i = rows - 1; i >= 1; i--) {
for (int j = 1; j <= rows - i; j++)
cout << " ";
for (int j = 1; j <= 2 * i - 1; j++)
cout << "*";
cout << endl;
}

return 0;
}

Output:

Rows for pattern: 5


*
***
*****
*******
*********
*******
*****
***
*

=== Code Execution Successful ===

Code explanation:
We first take the number of rows as input from the user to decide the
size of the pattern.
We use loops to print spaces and stars (*), first creating the upper
part of the diamond and then the lower part.
The stars increase in the upper part and decrease in the lower part,
forming a diamond-shaped star pattern in the output.

Option 6: Hollow Square Star Pattern in C++


Code:

https://www.wscubetech.com/resources/cpp/programs/star-pattern 6/20
6/22/25, 12:02 PM Star Pattern in C++ (15 Programs With Output)

#include <iostream>
using namespace std;

int main() {
int rows;
cout << "Rows for pattern: "; // Taking the input from use
cin >> rows;

for (int i = 1; i <= rows; i++) {


for (int j = 1; j <= rows; j++) {
if (i == 1 || i == rows || j == 1 || j == rows) //
cout << "*";
else
cout << " "; // Print space inside
}
cout << endl;
}
return 0;
}

Output:

Rows for pattern: 6


******
* *
* *
* *
* *
******

=== Code Execution Successful ===

Code Explanation:
We first take the number of rows as input from the user for the
pattern.
We use loops to print stars (*) on the border while keeping the inside
empty by printing spaces.
This creates a star pattern in C++ that looks like a hollow square, with
stars on the edges and spaces inside.

Option 7: Hollow Triangle Star Pattern in C++


Code:
https://www.wscubetech.com/resources/cpp/programs/star-pattern 7/20
6/22/25, 12:02 PM Star Pattern in C++ (15 Programs With Output)

#include <iostream>
using namespace std;

int main() {
int rows;
cout << "Rows for pattern: ";
cin >> rows;

for (int i = 1; i <= rows; i++) {


for (int j = 1; j <= i; j++) {
if (j == 1 || j == i || i == rows) // Print * on bo
cout << "*";
else
cout << " "; // Print space inside
}
cout << endl;
}
return 0;
}

Output:

Enter number of rows: 5


*
**
* *
* *
*****

=== Code Execution Successful ===

Code explanation:
We first take the number of rows as input from the user to decide the
size of the pattern.
We use loops to print stars (*) on the boundary while keeping the
inside empty by printing spaces.
This creates a star pattern in C++ that looks like a hollow right-angled
triangle, with stars on the edges and spaces inside.

Option 8: Right Arrow Star Pattern in C++


Code:

https://www.wscubetech.com/resources/cpp/programs/star-pattern 8/20
6/22/25, 12:02 PM Star Pattern in C++ (15 Programs With Output)

#include <iostream>
using namespace std;

int main() {
int rows = 5;

for (int i = 1; i <= rows; i++) {


for (int j = 1; j <= i; j++) // Increasing stars
cout << "*";
cout << endl;
}

for (int i = rows - 1; i >= 1; i--) {


for (int j = 1; j <= i; j++) // Decreasing stars
cout << "*";
cout << endl;
}

return 0;
}

Output:

*
**
***
****
*****
****
***
**
*

=== Code Execution Successful ===

Code explanation:
We first set the number of rows to 5, which decides the size of the
pattern.
We use loops to print an increasing number of stars (*) in the first half
and then a decreasing number of stars in the second half.

Option 9: Left Arrow Star Pattern in C++


Code:
https://www.wscubetech.com/resources/cpp/programs/star-pattern 9/20
6/22/25, 12:02 PM Star Pattern in C++ (15 Programs With Output)

#include <iostream>
using namespace std;

int main() {
int rows = 5;

for (int i = 1; i <= rows; i++) {


for (int j = 1; j <= rows - i; j++) // Spaces for align
cout << " ";
for (int j = 1; j <= i; j++) // Printing stars
cout << "*";
cout << endl;
}

for (int i = rows - 1; i >= 1; i--) {


for (int j = 1; j <= rows - i; j++) // Spaces for align
cout << " ";
for (int j = 1; j <= i; j++) // Printing stars
cout << "*";
cout << endl;
}

return 0;
}

Output:

*
**
***
****
*****
****
***
**
*

=== Code Execution Successful ===

Code explanation:
We first set the number of rows to 5, which decides the size of the
pattern.

https://www.wscubetech.com/resources/cpp/programs/star-pattern 10/20
6/22/25, 12:02 PM Star Pattern in C++ (15 Programs With Output)

We use loops to print spaces first, followed by stars (*), creating an


increasing pattern in the first half and a decreasing pattern in the
second half.
This forms a star pattern in C++ that looks like a left arrow, with stars
aligned diagonally in both directions.

Option 10: Hollow Pyramid Star Pattern in C++


Code:

#include <iostream>
using namespace std;

int main() {
int rows;
cout << "Rows for pattern: ";
cin >> rows;

for (int i = 1; i <= rows; i++) {


for (int j = 1; j <= rows - i; j++) // Spaces for align
cout << " ";
for (int j = 1; j <= 2 * i - 1; j++) { // Printing sta
if (j == 1 || j == 2 * i - 1 || i == rows) // Print
cout << "*";
else
cout << " "; // Print space inside
}
cout << endl;
}
return 0;
}

Output:

Rows for pattern: 7


*
* *
* *
* *
* *
* *
*************

=== Code Execution Successful ===

https://www.wscubetech.com/resources/cpp/programs/star-pattern 11/20
6/22/25, 12:02 PM Star Pattern in C++ (15 Programs With Output)

Code explanation:
We first take the number of rows as input from the user to decide the
size of the pattern.
We use loops to print spaces first, followed by stars (*) on the
boundary while keeping the inside empty.
This creates a hollow pyramid shape, where the stars form the outline,
and the inside remains empty.

Option 11: Butterfly Star Pattern in C++


Code:

#include <iostream>
using namespace std;

int main() {
int rows;
cout << "Rows for pattern: ";
cin >> rows;

// Upper half of the butterfly


for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= i; j++)
cout << "*";
for (int j = 1; j <= 2 * (rows - i); j++) // Spaces in
cout << " ";
for (int j = 1; j <= i; j++) // Right stars
cout << "*";
cout << endl;
}

// Lower half of the butterfly


for (int i = rows; i >= 1; i--) {
for (int j = 1; j <= i; j++)
cout << "*";
for (int j = 1; j <= 2 * (rows - i); j++)
cout << " ";
for (int j = 1; j <= i; j++)
cout << "*";
cout << endl;
}

return 0;
}

https://www.wscubetech.com/resources/cpp/programs/star-pattern 12/20
6/22/25, 12:02 PM Star Pattern in C++ (15 Programs With Output)

Output:

Rows for pattern: 6


* *
** **
*** ***
**** ****
***** *****
************
************
***** *****
**** ****
*** ***
** **
* *

=== Code Execution Successful ===

Code explanation:
We first take the number of rows as input from the user to determine
the size of the pattern.
The pattern has two halves, upper and lower, forming mirror images.
We use loops to print stars (*) on both sides and spaces in the middle
to create the butterfly shape.
This results in a star pattern in C++, where stars expand outward and
then shrink symmetrically.

Option 12: Hollow Butterfly Star Pattern in C++


Code:

#include <iostream>
using namespace std;

int main() {
int rows;
cout << "Rows for pattern: ";
cin >> rows;

// Upper half of the butterfly


for (int i = 1; i <= rows; i++) {
cout << "*";
for (int j = 1; j < i - 1; j++)
cout << " ";

https://www.wscubetech.com/resources/cpp/programs/star-pattern 13/20
6/22/25, 12:02 PM Star Pattern in C++ (15 Programs With Output)

if (i > 1) cout << "*"; // Second star if row > 1


for (int j = 1; j <= 2 * (rows - i); j++)
cout << " ";
cout << "*"; // Third star
for (int j = 1; j < i - 1; j++)
cout << " ";
if (i > 1) cout << "*";
cout << endl;
}

// Lower half of the butterfly


for (int i = rows; i >= 1; i--) {
cout << "*";
for (int j = 1; j < i - 1; j++)
cout << " ";
if (i > 1) cout << "*";
for (int j = 1; j <= 2 * (rows - i); j++)
cout << " ";
cout << "*";
for (int j = 1; j < i - 1; j++)
cout << " ";
if (i > 1) cout << "*";
cout << endl;
}

return 0;
}

Output:

Rows for pattern: 6


* *
** **
* * * *
* * * *
* * * *
* ** *
* ** *
* * * *
* * * *
* * * *
** **
* *

=== Code Execution Successful ===

https://www.wscubetech.com/resources/cpp/programs/star-pattern 14/20
6/22/25, 12:02 PM Star Pattern in C++ (15 Programs With Output)

Code explanation:
We first take the number of rows as input from the user to decide the
pattern size.
The pattern is divided into two upper and lower halves, forming a
symmetrical butterfly shape.
We use loops to print stars (*) on the edges while keeping spaces
inside, giving it a hollow effect.
This creates a star pattern program in C++, making a unique butterfly
outline with hollow spaces in the middle.

Option 13: Plus Star Pattern in C++


Code:

#include <iostream>
using namespace std;

int main() {
int size;
cout << "Rows for pattern: ";
cin >> size;

// Ensure the size is odd for symmetry


if (size % 2 == 0) {
cout << "Please enter an odd number for symmetry." << e
return 0;
}

int mid = size / 2; // Find the middle index

for (int i = 0; i < size; i++) {


for (int j = 0; j < size; j++) {
// Print '*' if it's the middle row or middle colum
if (i == mid || j == mid)
cout << "* ";
else
cout << " "; // Print space otherwise
}
cout << endl;
}

return 0;
}

Output:
https://www.wscubetech.com/resources/cpp/programs/star-pattern 15/20
6/22/25, 12:02 PM Star Pattern in C++ (15 Programs With Output)

Rows for pattern: 5


*
*
* * * * *
*
*

=== Code Execution Successful ===

Code explanation:
We first take the number of rows as input and ensure it is an odd
number for symmetry.
We use loops to print stars (*) in the middle row and middle column
while keeping spaces elsewhere.
This creates a plus-shaped pattern, where the stars form a cross in
the center of the given size.

Option 14: Hollow Diamond Star Pattern in C++


Code:

#include <iostream>
using namespace std;

int main() {
int rows;
cout << "Rows for pattern: ";
cin >> rows;

// Upper half of diamond


for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= rows - i; j++)
cout << " ";
cout << "*";
if (i > 1) {
for (int j = 1; j <= 2 * (i - 1) - 1; j++)
cout << " ";
cout << "*";
}
cout << endl;
}

// Lower half of diamond


for (int i = rows - 1; i >= 1; i--) {
for (int j = 1; j <= rows - i; j++)
https://www.wscubetech.com/resources/cpp/programs/star-pattern 16/20
6/22/25, 12:02 PM Star Pattern in C++ (15 Programs With Output)

cout << " ";


cout << "*";
if (i > 1) {
for (int j = 1; j <= 2 * (i - 1) - 1; j++)
cout << " ";
cout << "*";
}
cout << endl;
}

return 0;
}

Output:

Rows for pattern: 5


*
* *
* *
* *
* *
* *
* *
* *
*

=== Code Execution Successful ===

Code Explanation:
We first take the number of rows as input from the user to decide the
pattern size.
We use loops to print spaces first, then stars (*) on the boundary
while keeping the inside empty.
The star pattern program in C++ prints a hollow diamond, with stars
forming the outer shape and spaces inside.
The first half prints the upper part, and the second half completes the
lower part of the diamond.

Option 15: Cross Star Pattern in C++


Code:

https://www.wscubetech.com/resources/cpp/programs/star-pattern 17/20
6/22/25, 12:02 PM Star Pattern in C++ (15 Programs With Output)

#include <iostream>
using namespace std;

int main() {
int rows;
cout << "Rows for pattern: ";
cin >> rows ;

for (int i = 1; i <= rows; i++) {


for (int j = 1; j <= rows; j++) {
if (i == j || j == (rows - i + 1)) // Print * on di
cout << "*";
else
cout << " "; // Print space elsewhere
}
cout << endl;
}
return 0;
}

Output:

Rows for pattern: 5


* *
* *
*
* *
* *

=== Code Execution Successful ===

Code Explanation:
We first take the number of rows as input from the user to decide the
pattern size.
We use loops to print stars (*) on both diagonals while keeping
spaces elsewhere.
This forms an "X" shape, where stars appear at the positions where
the row and column numbers match or add up to the given rows.

FAQs About C++ Star Pattern Program

https://www.wscubetech.com/resources/cpp/programs/star-pattern 18/20
6/22/25, 12:02 PM Star Pattern in C++ (15 Programs With Output)

How can I create a star pattern in C++ using a loop?


You can use nested for loop to print a star in C++. The outer loop
controls the rows, while the inner loop prints stars and spaces to
create different shapes like pyramids or triangles.

What are some common types of star pattern programs in


C++?

How can I start learning patterns in C++?

Why should I practice patterns in C++?

Elevate Your Learning Journey with Cutting-Edge


Education Technology.

Company Our Programs


Contact Data
About Digital Marketing
https://www.wscubetech.com/resources/cpp/programs/star-pattern 19/20
6/22/25, 12:02 PM Star Pattern in C++ (15 Programs With Output)

WsCube Tech Blog Web Development


Self-Paced Courses Cyber Security
Events App Development

Support Telegram Community


Privacy Policy
Terms & Conditions
Refund Policy

© Copyright 2025, All Rights Reserved By WsCube Tech Version 3.0.29

https://www.wscubetech.com/resources/cpp/programs/star-pattern 20/20

You might also like