Search This Blog
Welcome to 1printf(), your ultimate destination for C, C++, Linux, Data Structures, and Microcontroller programming! 🚀 🔹Learn advanced coding techniques in C& C++ 🔹Master Linux internals & shell scripting 🔹Deep dive into Data Structures & Algorithms 🔹Explore Embedded Systems & Microcontrollers (8051,UART, RTOS) 🔹Get hands-on coding tutorials, project ideas,and interview preparation tips Whether you're a beginner or an experienced programmer, this channel will help you
Featured
- Get link
- X
- Other Apps
C++ Triangle Pattern Programs 5: Star, Number, Alphabet & Binary Patterns with Output
C++ Program to Print Consecutive Number Pyramid Pattern
A counter starts at 0 and keeps increasing across rows, so the numbers never repeat.
Source Code
- #include<iostream>
- using namespace std;
- int main()
- {
- int num,a=0;
- cout<<"Enter the number:"<<endl;
- cin>>num;
- for(int i=1;i<=num;i++)
- {
- // leading spaces
- for(int k=num;k>=i;k--)
- {
- cout<<" ";
- }
- // print i numbers, counter continues
- for(int j=1;j<=i;j++)
- {
- cout<<a++<<" ";
- }
- cout<<endl;
- }
- return 0;
- }
Output (Input: 5)
Enter the number:
5
0
1 2
3 4 5
6 7 8 9
10 11 12 13 14
How It Works
The variable a starts at 0 and is declared outside both loops, so it is never reset. The expression a++ prints the current value and then adds 1, which is why the numbers keep counting from one row to the next.
The outer loop i controls the row number. The k loop prints num - i + 1 spaces, and the j loop runs i times, so row i holds i numbers.
Once the numbers reach two digits (10 and above), each entry is wider than one character, so the rows no longer line up neatly. For a tidy shape with larger inputs, print with setw() from <iomanip>.
Dry Run (num = 5)
| i | spaces | a at start | numbers printed | a at end |
|---|---|---|---|---|
| 1 | 5 | 0 | 0 | 1 |
| 2 | 4 | 1 | 1 2 | 3 |
| 3 | 3 | 3 | 3 4 5 | 6 |
| 4 | 2 | 6 | 6 7 8 9 | 10 |
| 5 | 1 | 10 | 10 11 12 13 14 | 15 |
Algorithm
- Read
numand seta = 0. - Repeat for
ifrom 1 tonum. - Print a space for
kfromnumdown toi. - For
jfrom 1 toi, printaand a space, then increaseaby 1. - Move to the next line.
Time and Space Complexity
Search Description, Keywords and Hashtags
Description: C++ program to print a consecutive number pyramid pattern starting from 0, using nested for loops and a counter variable.
Keywords: C++ consecutive number pyramid, continuous number pattern in C++, C++ pyramid pattern program, counter variable in C++, C++ nested loops, C++ for loop pattern, pattern printing in C++, beginner C++ programs
Hashtags: #CppProgramming #Cpp #PatternPrograms #NumberPattern #ConsecutiveNumbers #PyramidPattern #NestedLoops #ForLoop #Coding #LearnCpp #1printf
C++ Program to Print Star Pyramid Pattern
One star in row 1, two in row 2, and one more star in every row after that.
Source Code
- #include<iostream>
- using namespace std;
- int main()
- {
- int num;
- cout<<"Enter the number:"<<endl;
- cin>>num;
- for(int i=1;i<=num;i++)
- {
- // leading spaces
- for(int k=num;k>=i;k--)
- {
- cout<<" ";
- }
- // print i stars
- for(int j=1;j<=i;j++)
- {
- cout<<"* ";
- }
- cout<<endl;
- }
- return 0;
- }
Output (Input: 5)
Enter the number:
5
*
* *
* * *
* * * *
* * * * *
How It Works
The outer loop i controls the row number. The k loop prints num - i + 1 spaces, so the rows shift left as they get longer.
The j loop runs i times and prints "* " each time, so row 1 has one star, row 2 has two, and so on. Then endl starts a new line.
Because a space is 1 character wide and each star with its trailing space is 2 characters, the shape leans slightly instead of forming a perfectly symmetric pyramid. To make it symmetric, print two spaces per indent step.
Dry Run (num = 5)
| i | spaces | stars | row printed |
|---|---|---|---|
| 1 | 5 | 1 | * |
| 2 | 4 | 2 | * * |
| 3 | 3 | 3 | * * * |
| 4 | 2 | 4 | * * * * |
| 5 | 1 | 5 | * * * * * |
Algorithm
- Read
numfrom the user. - Repeat for
ifrom 1 tonum. - Print a space for
kfromnumdown toi. - Print
"* "forjfrom 1 toi. - Move to the next line.
Time and Space Complexity
Search Description, Keywords and Hashtags
Description: C++ program to print a star pyramid pattern using nested for loops, with one loop for leading spaces and one loop for stars.
Keywords: C++ star pyramid, star pyramid pattern in C++, C++ pyramid pattern program, star pattern in C++, C++ nested loops, C++ for loop pattern, pattern printing in C++, beginner C++ programs
Hashtags: #CppProgramming #Cpp #PatternPrograms #StarPattern #StarPyramid #PyramidPattern #NestedLoops #ForLoop #Coding #LearnCpp #1printf
C++ Program to Print Alternating Row 1 0 Pyramid Pattern
Each row repeats a single value, and the value flips between 1 and 0 from row to row.
Source Code
- #include<iostream>
- using namespace std;
- int main()
- {
- int num;
- cout<<"Enter the number:"<<endl;
- cin>>num;
- for(int i=1;i<=num;i++)
- {
- // leading spaces
- for(int k=num;k>=i;k--)
- {
- cout<<" ";
- }
- // print i % 2, i times
- for(int j=1;j<=i;j++)
- {
- cout<<i%2<<" ";
- }
- cout<<endl;
- }
- return 0;
- }
Output (Input: 5)
Enter the number:
5
1
0 0
1 1 1
0 0 0 0
1 1 1 1 1
How It Works
The outer loop i controls the row number. The k loop prints num - i + 1 spaces, so the rows shift left as they get longer.
The j loop runs i times but prints i % 2, not j % 2. The value of i stays the same during a row, so the whole row has one value: 1 when i is odd and 0 when i is even.
Because a space is 1 character wide and each value with its trailing space is 2 characters, the shape leans slightly instead of forming a perfectly symmetric pyramid.
Dry Run (num = 5)
| i | spaces | i % 2 | times | row printed |
|---|---|---|---|---|
| 1 | 5 | 1 | 1 | 1 |
| 2 | 4 | 0 | 2 | 0 0 |
| 3 | 3 | 1 | 3 | 1 1 1 |
| 4 | 2 | 0 | 4 | 0 0 0 0 |
| 5 | 1 | 1 | 5 | 1 1 1 1 1 |
Algorithm
- Read
numfrom the user. - Repeat for
ifrom 1 tonum. - Print a space for
kfromnumdown toi. - Print
i % 2and a space,itimes. - Move to the next line.
Time and Space Complexity
Search Description, Keywords and Hashtags
Description: C++ program to print an alternating row 1 0 pyramid pattern using nested for loops and the modulus operator on the row number.
Keywords: C++ 1 0 pyramid pattern, C++ binary pyramid, alternating row 1 0 pattern in C++, C++ modulus operator pattern, C++ nested loops, C++ for loop pattern, pattern printing in C++, beginner C++ programs
Hashtags: #CppProgramming #Cpp #PatternPrograms #BinaryPattern #01Pattern #PyramidPattern #NestedLoops #ModulusOperator #Coding #LearnCpp #1printf
C++ Program to Print Alternating 1 0 Pyramid Pattern
Every row starts with 1 and alternates 1 0 1 0, growing by one value per row.
Source Code
- #include<iostream>
- using namespace std;
- int main()
- {
- int num;
- cout<<"Enter the number:"<<endl;
- cin>>num;
- for(int i=1;i<=num;i++)
- {
- // leading spaces
- for(int k=num;k>=i;k--)
- {
- cout<<" ";
- }
- // print 1 or 0 using j % 2
- for(int j=1;j<=i;j++)
- {
- cout<<j%2<<" ";
- }
- cout<<endl;
- }
- return 0;
- }
Output (Input: 5)
Enter the number:
5
1
1 0
1 0 1
1 0 1 0
1 0 1 0 1
How It Works
The outer loop i controls the row number. The k loop prints num - i + 1 spaces, so the rows shift left as they get longer.
The j loop runs from 1 to i and prints j % 2. When j is odd, the remainder is 1; when j is even, it is 0. Since j restarts at 1 in every row, each row begins with 1 and alternates.
Because a space is 1 character wide and each value with its trailing space is 2 characters, the shape leans slightly instead of forming a perfectly symmetric pyramid.
Dry Run (num = 5)
| i | spaces | j values | j % 2 printed |
|---|---|---|---|
| 1 | 5 | 1 | 1 |
| 2 | 4 | 1 2 | 1 0 |
| 3 | 3 | 1 2 3 | 1 0 1 |
| 4 | 2 | 1 2 3 4 | 1 0 1 0 |
| 5 | 1 | 1 2 3 4 5 | 1 0 1 0 1 |
Algorithm
- Read
numfrom the user. - Repeat for
ifrom 1 tonum. - Print a space for
kfromnumdown toi. - For
jfrom 1 toi, printj % 2and a space. - Move to the next line.
Time and Space Complexity
Search Description, Keywords and Hashtags
Description: C++ program to print an alternating 1 0 pyramid pattern using nested for loops and the modulus operator.
Keywords: C++ 1 0 pyramid pattern, C++ binary pyramid, alternating 1 0 pattern in C++, C++ modulus operator pattern, C++ nested loops, C++ for loop pattern, pattern printing in C++, beginner C++ programs
Hashtags: #CppProgramming #Cpp #PatternPrograms #BinaryPattern #01Pattern #PyramidPattern #NestedLoops #ModulusOperator #Coding #LearnCpp #1printf
C++ Program to Print Repeated Number Pyramid Pattern
Row 1 prints 1 once, row 2 prints 2 twice, row 3 prints 3 three times, and so on.
Source Code
- #include<iostream>
- using namespace std;
- int main()
- {
- int num;
- cout<<"Enter the number:"<<endl;
- cin>>num;
- for(int i=1;i<=num;i++)
- {
- // leading spaces
- for(int k=num;k>=i;k--)
- {
- cout<<" ";
- }
- // print i, i times
- for(int j=1;j<=i;j++)
- {
- cout<<i<<" ";
- }
- cout<<endl;
- }
- return 0;
- }
Output (Input: 5)
Enter the number:
5
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
How It Works
The outer loop i controls the row number. The k loop prints num - i + 1 spaces, so the rows shift left as they get longer.
The j loop only decides how many times to print. It runs i times, but it prints the value of i (not j), so every number in a row is the same. That is the only difference from the 1 2 3 pyramid.
Because a space is 1 character wide and each number with its trailing space is 2 characters, the shape leans slightly instead of forming a perfectly symmetric pyramid.
Dry Run (num = 5)
| i | spaces | value | times |
|---|---|---|---|
| 1 | 5 | 1 | 1 |
| 2 | 4 | 2 | 2 |
| 3 | 3 | 3 | 3 |
| 4 | 2 | 4 | 4 |
| 5 | 1 | 5 | 5 |
Algorithm
- Read
numfrom the user. - Repeat for
ifrom 1 tonum. - Print a space for
kfromnumdown toi. - Print the value of
iand a space,itimes. - Move to the next line.
Time and Space Complexity
Search Description, Keywords and Hashtags
Description: C++ program to print a repeated number pyramid pattern using nested for loops, where each row prints its own row number.
Keywords: C++ repeated number pyramid, number pyramid pattern in C++, C++ pyramid pattern program, same number in each row, C++ nested loops, C++ for loop pattern, pattern printing in C++, beginner C++ programs
Hashtags: #CppProgramming #Cpp #PatternPrograms #NumberPattern #PyramidPattern #NestedLoops #ForLoop #Coding #LearnCpp #1printf
C++ Program to Print Increasing Number Pyramid Pattern
Nested for loops · one loop for spaces, one loop for numbers
Source Code
#include<iostream>
using namespace std;
int main()
{
int num;
cout<<"Enter the number:"<<endl;
cin>>num;
for(int i=1;i<=num;i++)
{
// leading spaces
for(int k=num;k>=i;k--)
{
cout<<" ";
}
// numbers 1 to i
for(int j=1;j<=i;j++)
{
cout<<j<<" ";
}
cout<<endl;
}
return 0;
}
Output
Enter the number:
5
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
How It Works
The outer loop i controls the row. For every row, the k loop prints num - i + 1 spaces, and the j loop prints the numbers 1 to i. Then endl starts a new line.
Because each space is one character but each number takes two (digit plus space), the pyramid leans slightly: the left edge moves one step per row and the right edge moves two.
Dry Run (num = 5)
| i | spaces | numbers printed |
|---|---|---|
| 1 | 5 | 1 |
| 2 | 4 | 1 2 |
| 3 | 3 | 1 2 3 |
| 4 | 2 | 1 2 3 4 |
| 5 | 1 | 1 2 3 4 5 |
Algorithm
- Read
numfrom the user. - Repeat for
ifrom 1 tonum: - Print a space for
kfromnumdown toi. - Print
jand a space forjfrom 1 toi. - Move to the next line.
Complexity
Search Description
C++ program to print an increasing number pyramid pattern using nested for loops. Learn how a space loop and a number loop work together to build pyramid patterns.
Keywords
C++ number pyramid, increasing number pyramid pattern, C++ pyramid pattern program, number pattern in C++, C++ nested loops, C++ for loop pattern, pattern printing in C++, beginner C++ programs
Hashtags
#CppProgramming #Cpp #PatternPrograms #NumberPattern #PyramidPattern #NestedLoops #ForLoop #Coding #LearnCpp #1printf
Popular Posts
C++ Program for Hybrid Inheritance (All Types Together)
- Get link
- X
- Other Apps
C++ Program for Function Overloading Example
- Get link
- X
- Other Apps
Comments
Post a Comment