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