Here are C++ program to generate different pyramid of numbers.
Method 1 : C++ Program to print Pyramid
#include<iostream>
using namespace std;
int main()
{
int row,i,j,k;
cout<<"Enter the value ";
cin>>row;
for(i=0;i<row;i++)
{
for(j=row;j>i;j--)
cout<<" ";
for(k=0;k<=i;k++)
cout<<"* ";
cout<<endl;
}
return 0;
}
using namespace std;
int main()
{
int row,i,j,k;
cout<<"Enter the value ";
cin>>row;
for(i=0;i<row;i++)
{
for(j=row;j>i;j--)
cout<<" ";
for(k=0;k<=i;k++)
cout<<"* ";
cout<<endl;
}
return 0;
}
OUTPUT
Method 2 : C++ Program to print Pyramid using recursion
#include<iostream>
using namespace std;
void pyra3(int k,int i)
{
if(k>i)
return;
cout<<"* ";
pyra3(k+1,i);
}
void pyra2(int j,int i)
{
if(j==i)
return;
cout<<" ";
pyra2(j-1,i);
}
void pyra1(int row,int i)
{
if(i==row)
return;
pyra2(row,i);
pyra3(0,i);
cout<<endl;
pyra1(row,i+1);
}
int main()
{
int row;
cout<<"Enter the value ";
cin>>row;
pyra1(row,0);
return 0;
}
using namespace std;
void pyra3(int k,int i)
{
if(k>i)
return;
cout<<"* ";
pyra3(k+1,i);
}
void pyra2(int j,int i)
{
if(j==i)
return;
cout<<" ";
pyra2(j-1,i);
}
void pyra1(int row,int i)
{
if(i==row)
return;
pyra2(row,i);
pyra3(0,i);
cout<<endl;
pyra1(row,i+1);
}
int main()
{
int row;
cout<<"Enter the value ";
cin>>row;
pyra1(row,0);
return 0;
}
OUTPUT
C++ Program for Pyramid of numbers 1
#include<iostream>
using namespace std;
int main()
{
int row,i,j,k,s=1;
cout<<"Enter the value ";
cin>>row;
for(i=0;i<row;i++)
{
for(j=2*row+1;j>2*i+1;j--)
cout<<" ";
for(k=1;k<=2*i+1;k++,s++)
{
if(s>9)
s=1;
cout<<s<<" ";
}
cout<<endl;
}
return 0;
}
using namespace std;
int main()
{
int row,i,j,k,s=1;
cout<<"Enter the value ";
cin>>row;
for(i=0;i<row;i++)
{
for(j=2*row+1;j>2*i+1;j--)
cout<<" ";
for(k=1;k<=2*i+1;k++,s++)
{
if(s>9)
s=1;
cout<<s<<" ";
}
cout<<endl;
}
return 0;
}
OUTPUT
C++ Program for Pyramid of numbers 2
#include<iostream>
using namespace std;
int main()
{
int row;
cout<<"Enter the value ";
cin>>row;
for(int i=0;i<=row;i++)
{
for(int y=1;y<=i;y++)
cout<<y<<" ";
cout<<endl;
}
return 0;
}
using namespace std;
int main()
{
int row;
cout<<"Enter the value ";
cin>>row;
for(int i=0;i<=row;i++)
{
for(int y=1;y<=i;y++)
cout<<y<<" ";
cout<<endl;
}
return 0;
}
OUTPUT
C++ Program for Pyramid of numbers 3
#include<iostream>
using namespace std;
int main()
{
int row,s;
cout<<"Enter the value ";
cin>>row;
for(int i=0;i<row;i++)
{
s=row-i;
for(int k=2*i+1;k<=2*row+1;k++)
cout<<" ";
for(int y=0;y<=i;y++,s++)
cout<<s<<" ";
--s;
for(int z=0;z<i;z++)
cout<<--s<<" ";
cout<<endl;
}
return 0;
}
using namespace std;
int main()
{
int row,s;
cout<<"Enter the value ";
cin>>row;
for(int i=0;i<row;i++)
{
s=row-i;
for(int k=2*i+1;k<=2*row+1;k++)
cout<<" ";
for(int y=0;y<=i;y++,s++)
cout<<s<<" ";
--s;
for(int z=0;z<i;z++)
cout<<--s<<" ";
cout<<endl;
}
return 0;
}
OUTPUT
C++ Program for Pyramid of numbers 4
#include<iostream>
using namespace std;
int main()
{
int row;
cout<<"Enter the value ";
cin>>row;
for(int i=0;i<=row;i++)
{
for(int k=0;k<=i;k++)
cout<<" ";
for(int y=1;y<=row-i;y++)
cout<<y<<" ";
cout<<endl;
}
return 0;
}
using namespace std;
int main()
{
int row;
cout<<"Enter the value ";
cin>>row;
for(int i=0;i<=row;i++)
{
for(int k=0;k<=i;k++)
cout<<" ";
for(int y=1;y<=row-i;y++)
cout<<y<<" ";
cout<<endl;
}
return 0;
}
OUTPUT
C++ Program for Pyramid of numbers 5
#include<iostream>
using namespace std;
int main()
{
int row;
cout<<"Enter the value ";
cin>>row;
for(int i=1;i<=row;i++)
{
for(int k=row;k>=i;k--)
cout<<" ";
for(int y=1;y<=i;y++)
cout<<i<<" ";
cout<<endl;
}
return 0;
}
using namespace std;
int main()
{
int row;
cout<<"Enter the value ";
cin>>row;
for(int i=1;i<=row;i++)
{
for(int k=row;k>=i;k--)
cout<<" ";
for(int y=1;y<=i;y++)
cout<<i<<" ";
cout<<endl;
}
return 0;
}
OUTPUT
C++ Program for Pyramid of numbers 6
#include<iostream>
using namespace std;
int main()
{
long num,s,y=1;
cout<<"enter the number ";
cin>>num;
for(int i=0;i<num;i++)
{
s=i;
for(int k=num;k>i;k--)
cout<<" ";
for(y=1;y<=i;y++,s++)
cout<<s<<" ";
--s;
for(int z=1;z<i;z++)
cout<<--s<<" ";
cout<<endl;
}
return 0;
}
using namespace std;
int main()
{
long num,s,y=1;
cout<<"enter the number ";
cin>>num;
for(int i=0;i<num;i++)
{
s=i;
for(int k=num;k>i;k--)
cout<<" ";
for(y=1;y<=i;y++,s++)
cout<<s<<" ";
--s;
for(int z=1;z<i;z++)
cout<<--s<<" ";
cout<<endl;
}
return 0;
}
No comments:
Post a Comment