#include<iostream> //for input/output
using namespace std;
int main()
{
//decalaring variables
int height, //for height of the output pattern
width; //for width of the output pattern
cout << "The value of height and width should be an odd number and greater than two." //prompting the user
<< endl;
cout << "Enter the height of the output to be printed: "; //prompting the user for height
cin >> height;
while( height < 3 || height%2==0 ) //loop for validating the input for height
{
cout << "Invalid input!"
<< "Enter again: ";
cin >> height;
}
cout << "Enter the width of the output to be printed: "; //prompting the user for width
cin >> width;
while( width < 5 || width%2==0 ) //loop for validating the input for width
{
cout << "Invalid input!"
<< "Enter again: ";
cin >> width;
}
int i=1, //declaring and intializing loop control variable
j;
cout << "The desired output pattern is...\n"; //prompting the user to show output
while( i <= height )
{
j=1; //initializing nested loop control variable
//applying conditions for printing the desired formate
if( i%2 != 0 ) //validating the height as odd
{
while( j<=width )
{
cout << "+"; //print a '+' sign
j++;
}
}
else
{
while( j<=(width/2+1) )
{
cout << "+ "; //printing '+' with a space
j++;
}
}
cout << endl;
i++;
}
return 0;
}
No comments:
Post a Comment