In this, we are going to see how to print the above pattern of pyramid/triangle of numbers in reverse order, using nested for loops in C++.
The code given below can be used for TURBO C++ Compiler:-
#include <iostream.h>
#include <conio.h>
void main()
{
clrscr();
int i, j, k, l, n;
cout << "\nEnter the Range\n";
cin >> n;
for (i = 1; i <= n; i++)
{
for (j = 1; j <= n - i; j++)
{
cout << " ";
}
for (k = i; k >= 1; --k)
{
cout << k;
}
for (l = 2; l < i + 1; ++l)
{
cout << l;
}
cout << "\n";
}
getch();
}
The code given below can be used for g++/gcc Compiler:-
#include <iostream>
using namespace std;
int main()
{
int i, j, k, l, n;
cout << "\nEnter the Range\n";
cin >> n;
for (i = 1; i <= n; i++)
{
for (j = 1; j <= n - i; j++)
{
cout << " ";
}
for (k = i; k >= 1; --k)
{
cout << k;
}
for (l = 2; l < i + 1; ++l)
{
cout << l;
}
cout << "\n";
}
return 0;
}
#ENJOY CODING
Â
Post a Comment
FOR ANY DOUBTS AND ERRORS FEEL FREE TO ASK. YOUR DOUBTS WILL BE ADDRESSED ASAP