Monday, November 10, 2014

C++ program for printing a Diamond pattern

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

int main()
{
int StarinLine=-1,
StarVariable,
space,
N;
cout << "Enter the number (should be an Odd Number) for the height of the Diamond: ";
cin >> N;
while( N<=0 || N%2==0 )
{
cout << "Wrong Input! Enter again: ";
cin >> N;
}

cout << "The Diamond Pattern is:\n";
space=N;
for( int i=1 ; i<=N ; i++)
{
if( i<=(N/2+1) )
{
StarinLine+=2;
space-=1;
cout << setw(space);
for( StarVariable=1 ; StarVariable<=StarinLine ; StarVariable++ )
{
cout << "*";
}
}
else
{
StarinLine-=2;
space+=1;
cout << setw(space);
for( StarVariable=1 ; StarVariable<=StarinLine ; StarVariable++ )
{
cout << "*";
}
}

cout << endl;
}

return 0;
}

No comments:

Post a Comment