Sunday, November 30, 2014

C++ program that creates four different patterns of different types

#include<iostream>
#include<iomanip>
using namespace std;

int main()
{
unsigned int N,
i,j,
p2,p3,p4;
cout << "Enter the size between 2to9: ";
cin >> N;
while( N<2||N>9 )
{
cout << "Wrong Input! (should be between 2to9)"
<< "\nEnter again: ";
cin >> N;
}
cout << "Pattern 1";
cout << setw(15)
<< "Pattern 2"
<< setw(15)
<< "Pattern 3"
<< setw(15)
<< "Pattern 4"
<< endl;
p2=N;
p3=N;
p4=0;
for( i=1 ; i<=N ; i++ )
{
for( j=1 ; j<=N ; j++ )
{
if( j==i )
cout << N;
else
cout << "$";
}
cout << setw(16-N);
for( j=1 ; j<=N ; j++ )
{
if(j==p2 )
cout << N;
else
cout << "$";
}
p2-=1;
cout << setw(16-N);
for( j=1 ; j<=N ; j++ )
{
if( j<=p3 )
cout << "$";
else
cout << N;
}
p3-=1;
cout << setw(16-N);
for( j=1 ; j<=N ; j++ )
{
if( j<=p4 )
cout << N;
else
cout << "$";
}
p4+=1;
cout << endl;
}


return 0;
}

No comments:

Post a Comment