C++ program to print Diamond pattern using for-loops, recursion. I have also given different ways to print diamond and different patterns other than diamond like Right angled Triangle, Star Pattern, Trapezium, Hexagon.
C++ program to print Diamond pattern using for-loops
#include<iostream>
using namespace std;
int main()
{
int row,i,j,k;
cout<<"Enter the value ";
cin>>row;
row/=2;
for(i=0;i<=row;i++)
{
for(j=row;j>i;j--)
cout<<" ";
for(k=0;k<i;k++)
cout<<"* ";
cout<<endl;
}
for(i=1;i<row;i++)
{
for(j=0;j<i;j++)
cout<<" ";
for(k=row;k>i;k--)
cout<<"* ";
cout<<endl;
}
return 0;
}
using namespace std;
int main()
{
int row,i,j,k;
cout<<"Enter the value ";
cin>>row;
row/=2;
for(i=0;i<=row;i++)
{
for(j=row;j>i;j--)
cout<<" ";
for(k=0;k<i;k++)
cout<<"* ";
cout<<endl;
}
for(i=1;i<row;i++)
{
for(j=0;j<i;j++)
cout<<" ";
for(k=row;k>i;k--)
cout<<"* ";
cout<<endl;
}
return 0;
}
Output
C++ program to print Diamond pattern using recursion
#include<iostream>
using namespace std;
void daim3(int k,int i,int z)
{
if(k==i)
return;
cout<<"* ";
daim3(k+z,i,z);
}
void daim2(int j,int i,int z)
{
if(j==i)
return;
cout<<" ";
daim2(j+z,i,z);
}
void daim1(int row,int i)
{
if(i>row)
return;
daim2(row,i,-1);
daim3(0,i,1);
cout<<endl;
daim1(row,i+1);
}
void daim4(int row,int i)
{
if(i>row)
return;
daim2(0,i,1);
daim3(row,i,-1);
cout<<endl;
daim4(row,i+1);
}
int main()
{
int row,i,j,k;
cout<<"Enter the value ";
cin>>row;
daim1(row,0);
daim4(row,1);
return 0;
}
using namespace std;
void daim3(int k,int i,int z)
{
if(k==i)
return;
cout<<"* ";
daim3(k+z,i,z);
}
void daim2(int j,int i,int z)
{
if(j==i)
return;
cout<<" ";
daim2(j+z,i,z);
}
void daim1(int row,int i)
{
if(i>row)
return;
daim2(row,i,-1);
daim3(0,i,1);
cout<<endl;
daim1(row,i+1);
}
void daim4(int row,int i)
{
if(i>row)
return;
daim2(0,i,1);
daim3(row,i,-1);
cout<<endl;
daim4(row,i+1);
}
int main()
{
int row,i,j,k;
cout<<"Enter the value ";
cin>>row;
daim1(row,0);
daim4(row,1);
return 0;
}
Output
C++ program to print another Diamond pattern using for-loops
#include<iostream>
using namespace std;
int main()
{
int row=6,i,j,k;
cout<<"Enter the value ";
cin>>row;
row/=2;
for(j=row;j>0;j--)
cout<<" ";
cout<<"*"<<endl;
for(i=1;i<row;i++)
{
for(j=row;j>i;j--)
cout<<" ";
cout<<"*";
for(k=1;k<i+j;k++)
cout<<" ";
cout<<"*";
cout<<endl;
}
for(i=0;i<row;i++)
{
for(j=0;j<i;j++)
cout<<" ";
cout<<"*";
for(k=2*row-1;k>i+j;k--)
cout<<" ";
cout<<"*";
cout<<endl;
}
for(j=row;j>0;j--)
cout<<" ";
cout<<"*";
return 0;
}
using namespace std;
int main()
{
int row=6,i,j,k;
cout<<"Enter the value ";
cin>>row;
row/=2;
for(j=row;j>0;j--)
cout<<" ";
cout<<"*"<<endl;
for(i=1;i<row;i++)
{
for(j=row;j>i;j--)
cout<<" ";
cout<<"*";
for(k=1;k<i+j;k++)
cout<<" ";
cout<<"*";
cout<<endl;
}
for(i=0;i<row;i++)
{
for(j=0;j<i;j++)
cout<<" ";
cout<<"*";
for(k=2*row-1;k>i+j;k--)
cout<<" ";
cout<<"*";
cout<<endl;
}
for(j=row;j>0;j--)
cout<<" ";
cout<<"*";
return 0;
}
Output
C++ program to print another Hollow Diamond pattern using for-loops
#include<iostream>
using namespace std;
int main()
{
int i,j,k,l,row;
cout<<"Enter number of rows : ";
cin>>row;
cout<<endl;
row=row/2;
for(i=0;i<row;i++)
{
for(j=row;j>i;j--)
cout<<"*";
for(k=0;k<i*2;k++)
cout<<" ";
for(j=row;j>i;j--)
cout<<"*";
cout<<endl;
}
for(i=0;i<row;i++)
{
for(j=0;j<=i;j++)
cout<<"*";
for(k=row*2-2;k>i*2;k--)
cout<<" ";
for(j=0;j<=i;j++)
cout<<"*";
cout<<endl;
}
}
using namespace std;
int main()
{
int i,j,k,l,row;
cout<<"Enter number of rows : ";
cin>>row;
cout<<endl;
row=row/2;
for(i=0;i<row;i++)
{
for(j=row;j>i;j--)
cout<<"*";
for(k=0;k<i*2;k++)
cout<<" ";
for(j=row;j>i;j--)
cout<<"*";
cout<<endl;
}
for(i=0;i<row;i++)
{
for(j=0;j<=i;j++)
cout<<"*";
for(k=row*2-2;k>i*2;k--)
cout<<" ";
for(j=0;j<=i;j++)
cout<<"*";
cout<<endl;
}
}
Output
C++ program to print right angled triangle
#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+1;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+1;j--)
cout<<" ";
for(k=0;k<=i;k++)
cout<<"*";
cout<<endl;
}
return 0;
}
Output
C++ program to Star pattern using for-loops
#include<iostream>
using namespace std;
int main()
{
int row,i,j,k;
cout<<"Enter the value ";
cin>>row;
row-=row/2;
for(i=0;i<row;i++)
{
for(j=2*row;j>i;j--)
cout<<" ";
for(k=0;k<i*2+1;k++)
cout<<"*";
cout<<endl;
}
for(i=0;i<row;i++)
{
for(j=0;j<i;j++)
cout<<" ";
for(k=4*row+1;k>2*i;k--)
cout<<"*";
cout<<endl;
}
for(i=0;i<row+1;i++)
{
for(j=row;j>i;j--)
cout<<" ";
for(k=0;k<=2*(i+row);k++)
cout<<"*";
cout<<"\n";
}
for(i=0;i<row;i++)
{
for(j=0;j<i+row+1;j++)
cout<<" ";
for(k=2*row;k>2*i+1;k--)
cout<<"*";
cout<<endl;
}
return 0;
}
using namespace std;
int main()
{
int row,i,j,k;
cout<<"Enter the value ";
cin>>row;
row-=row/2;
for(i=0;i<row;i++)
{
for(j=2*row;j>i;j--)
cout<<" ";
for(k=0;k<i*2+1;k++)
cout<<"*";
cout<<endl;
}
for(i=0;i<row;i++)
{
for(j=0;j<i;j++)
cout<<" ";
for(k=4*row+1;k>2*i;k--)
cout<<"*";
cout<<endl;
}
for(i=0;i<row+1;i++)
{
for(j=row;j>i;j--)
cout<<" ";
for(k=0;k<=2*(i+row);k++)
cout<<"*";
cout<<"\n";
}
for(i=0;i<row;i++)
{
for(j=0;j<i+row+1;j++)
cout<<" ";
for(k=2*row;k>2*i+1;k--)
cout<<"*";
cout<<endl;
}
return 0;
}
Output
C++ program to print Trapezium pattern using for-loops
#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<=row+j;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<=row+j;k++)
cout<<"* ";
cout<<endl;
}
return 0;
}
Output
C++ program to print Parallelogram pattern using for-loops
#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<=2*row;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<=2*row;k++)
cout<<"* ";
cout<<endl;
}
return 0;
}
Output
C++ program to print Hexagon pattern using for-loops
#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<=row+j-1;k++)
cout<<"* ";
cout<<endl;
}
for(i=1;i<row;i++)
{
for(j=0;j<=i;j++)
cout<<" ";
for(k=0;k<=2*row-j-1;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<=row+j-1;k++)
cout<<"* ";
cout<<endl;
}
for(i=1;i<row;i++)
{
for(j=0;j<=i;j++)
cout<<" ";
for(k=0;k<=2*row-j-1;k++)
cout<<"* ";
cout<<endl;
}
return 0;
}
No comments:
Post a Comment