Today I want to tell you about how we can make different patterens in C++…..C++ is a computer programing language in which we makes desired programs . I am writing a program in which I have make different patterens like hourglass patteren, diamond patteren, nepalflag patteren and ladder patteren…so here we go…
#include<iostream>
using namespace std;
void main()
{
int choice,a,i,j;//names of variable
char b;//must be character
cout<<" Enter 1 to display hourglass pattern : \n";
cout<<" Enter 2 to display diamond pattern : \n";
cout<<" Enter 3 to display Nepal flag : \n";
cout<<" Enter 4 to display ladder pattern : \n";
cin>>choice;
switch (choice)//must be a number(1-3)
{
case 1:
cout<<" Choose the size of pattern : \n";//must enter the interger
cin>>a;//must be integer
cout<<" Select the character for display the pattern like(#, &, *, #, @) : \n";//either character or integer
cin>>b;//must be character
if(a<1)
{
cout<<" You entered irrelevent or negative number : \n";//user enter negative number
cout<<" Again enter number : \n"; //for enter new number
cin>>a;
}
if (a%2!=0)//hour glass display when user give odd number
{
for(int i=a;i>=1;i=i-2)
{
for(int j=a;j>=i;j--)
cout<<" ";
for(int j=1;j<=i;j++)
cout<<b<<" ";
cout<<endl;
}
for(int j=1;j<=a;j=j+2)
{
for(int i=a;i>=j;i--)
cout<<" ";
for(int i=1;i<=j;i=i++)
cout<<b<<" ";
cout<<endl;
}
}
else if (a%2==0)//hour glass display when user give odd number
{
for(int i=a;i>=1;i=i-2)
{
for(int j=a;j>=i;j--)
cout<<" ";
for(int j=1;j<=i;j++)
cout<<b<<" ";
cout<<endl;
}
for(int j=2;j<=a;j=j+2)
{
for(int i=a;i>=j;i--)
cout<<" ";
for(int i=1;i<=j;i++)
cout<<b<<" ";
cout<<endl;
}
}
break;//end of case 1
case 2:
cout<<" Choose the size of pattern : \n";//must enter the interger
cin>>a;//must be integer
cout<<" Select the character for display the pattern like(#, &, *, #, @) : \n";//either character or integer
cin>>b;//must be character
if(a<1)
{
cout<<" You entered irrelevent or negative number : \n";//user enter negative number
cout<<" Again enter number : \n"; //for enter new number
cin>>a;
cout<<endl;
}
{
for(int j=1;j<=a;j=j+2) //loop for diamond
{for(int i=a;i>=j;i--)
cout<<" ";
for(int i=1;i<=j;i=i++)
cout<<b<<" ";
cout<<endl<<endl;
}
for(int i=a;i>=1;i=i-2)
{
for(int j=a;j>=i;j--)
cout<<" ";
for(int j=1;j<=i;j++)
cout<<b<<" ";
cout<<endl<<endl;
}
}
break;
case 3:
cout<<" Choose the size of pattern : \n";//must enter the interger
cin>>a;//must be integer
cout<<" Select the character for display the pattern like(#, &, *, #, @) : \n";//either character or integer
cin>>b;//must be character
if(a<1)
{
cout<<" You entered irrelevent or negative number : \n";//user enter negative number
cout<<" Again enter number : \n"; //for enter new number
cin>>a;
cout<<endl;
}
{
for(int j=1;j<=a;j=j++) //loop for nepal flag
{
for(int i=a;i>=j;i--)
cout<<"";
for(int i=1;i<=j;i=i++)
cout<<b<<"";
cout<<endl<<endl;
}
for(int j=1;j<=a;j=j++)
{
for(int i=a;i>=j;i--)
cout<<"";
for(int i=a;i>=j;i=i--)
cout<<b<<"";
cout<<endl<<endl;
}
for(int j=1;j<=a;j=j++)
{
for(int i=a;i>=j;i--)
cout<<"";
for(int i=1;i<=j;i=i++)
cout<<b<<"";
cout<<endl<<endl;
}
for(int j=1;j<=a;j=j++)
{
for(int i=a;i>=j;i--)
cout<<"";
for(int i=a;i>=j;i=i--)
cout<<b<<"";
cout<<endl<<endl;
}
}
break;
case 4:
cout<<" Choose the size of pattern : \n";//must enter the interger
cin>>a;//must be integer
cout<<" Select the character for display the pattern like(#, &, *, #, @) : \n";//either character or integer
cin>>b;//must be character
if(a<1)
{
cout<<" You entered irrelevent or negative number : \n";//user enter negative number
cout<<" Again enter number : \n"; //for enter new number
cin>>a;
cout<<endl;
}
for(int i=1;i<=a;i++) //loop for ladeer pattern
{
for (int i=0;i<=5;i++)
{
cout<<b<<"";
}
cout<<endl;
for (int j=0;j<6;j++)
{
for (int i=0;i<2;i++)
cout<<b<<" "<<b<<endl;
}
}
for (int i=0;i<=5;i++)
{
cout<<b<<"";
}
cout<<endl;
break;
default:
cout<<"You entered a wrong choice"<<endl;//user entered wrong number
cout<<endl;
cout<<"Are you want to enter another number :";
cout<<endl;
system("pause");//for syster pause
}
}
writer: Waleed Altaf
You can also follow me on twitter: https://twitter.com/waleedannex64