Algorithm
Algorithm for C++ Program to Create a Pyramid:
1. Input: Accept the number of rows for the pyramid (rows).
2. Outer Loop: Run a loop from i = 1 to rows (inclusive).
a. Inner Loop (Spaces): Run a loop from j = 1 to rows - i (inclusive).
i. Print a space.
b. Inner Loop (Stars): Run a loop from k = 1 to 2 * i - 1 (inclusive).
i. Print an asterisk (*).
c. Move to the next line.
Algorithm for C++ Program to Create a Pattern:
1. Input: Accept the number of rows for the pattern (rows).
2. Outer Loop: Run a loop from i = 1 to rows (inclusive).
a. Inner Loop: Run a loop from j = 1 to i (inclusive).
i. Print the value of j followed by a space.
b. Move to the next line.
Code Examples
#1 Code Example- Programs to Print Triangles Using *, Numbers, and Characters
Code -
C++ Programming
#include <iostream>
using namespace std;
int main() {
int rows;
cout << "Enter number of rows: ";
cin >> rows;
for(int i = 1; i <= rows; ++i) {
for(int j = 1; j <= i; ++j) {
cout << "* ";
}
cout << "\n";
}
return 0;
}
Copy The Code &
Try With Live Editor
Output
* *
* * *
* * * *
* * * * *
#2 Code Example- Program to Print a Half-Pyramid Using Numbers
Code -
C++ Programming
#include <iostream>
using namespace std;
int main() {
int rows;
cout << "Enter number of rows: ";
cin >> rows;
for(int i = 1; i <= rows; ++i) {
for(int j = 1; j <= i; ++j) {
cout << j << " ";
}
cout << "\n";
}
return 0;
}
Copy The Code &
Try With Live Editor
Output
1 2
1 2 3
1 2 3 4
1 2 3 4 5
#3 Code Example- Program to Print Half-Pyramid Using Alphabets
Code -
C++ Programming
#include <iostream7gt;
using namespace std;
int main() {
char input, alphabet = 'A';
cout << "Enter the uppercase character you want to print in the last row: ";
cin >> input;
// convert input character to uppercase
input = toupper(input);
for(int i = 1; i <= (input-'A'+1); ++i) {
for(int j = 1; j <= i; ++j) {
cout << alphabet << " ";
}
++alphabet;
cout << endl;
}
return 0;
}
Copy The Code &
Try With Live Editor
Output
B B
C C C
D D D D
E E E E E
#4 Code Example-Programs to Print an Inverted Half-Pyramid Using * and Numbers
Code -
C++ Programming
#include <iostream>
using namespace std;
int main() {
int rows;
cout << "Enter number of rows: ";
cin >> rows;
for(int i = rows; i >= 1; --i) {
for(int j = 1; j <= i; ++j) {
cout << "* ";
}
cout << endl;
}
return 0;
}
Copy The Code &
Try With Live Editor
Output
* * * *
* * *
* *
*
#5 Inverted Half-Pyramid Using Numbers
Code -
C++ Programming
#include <iostream>
using namespace std;
int main() {
int rows;
cout << "Enter number of rows: ";
cin >> rows;
for(int i = rows; i >= 1; --i)
{
for(int j = 1; j <= i; ++j)
{
cout << j << " ";
}
cout << endl;
}
return 0;
}
Copy The Code &
Try With Live Editor
Output
1 2 3 4
1 2 3
1 2
1
#6 Programs to Display Pyramids and Inverted Pyramids Using * and Digits
Code -
C++ Programming
#include <iostream7gt;
using namespace std;
int main() {
int space, rows;
cout <<"Enter number of rows: ";
cin >> rows;
for(int i = 1, k = 0; i <= rows; ++i, k = 0) {
for(space = 1; space <= rows-i; ++space) {
cout <<" ";
}
while(k != 2*i-1) {
cout << "* ";
++k;
}
cout << endl;
}
return 0;
}
Copy The Code &
Try With Live Editor
Output
* * *
* * * * *
* * * * * * *
* * * * * * * * *
Demonstration
C++ Programs Example To Create Pyramid and Pattern-DevsEnv