Starric Pattern

10:38 0 Comments A+ a-

Problem :
Write a C++ program which prints the following pattern by taking the size (dimensions) from the user. For example, if the user enters the size to be 5, the following pattern should be displayed on screen:
*****
 *****
  *****
   *****
    *****
In order to display the above pattern, your program should use ONLY following THREE cout statements:
cout << "*"; cout << " "; cout << endl;
Input validation: Your program should make sure that the size is greater than or equal to 2. In case of invalid input, your program should keep prompting the user again and again till the user provides valid input.
Solution:
# include <iostream>
using namespace std;
int main()
{
int num; //num represents the size of pattern i.e no of rows and no of staric in them
cout<<"Enter the size of pattern (no of rows):"<<endl;
cin>>num; //size of pattern declared by input from user
while(num<2) //check for value of num if less than 2
{
cout<<"Input size wrong (must be greater or equal to 2)"<<endl;
cout<<"Enter the size of pattern again:"<<endl;
cin>>num;
}
for(int i=0;i<num;i++) //for loop to control rows
{
for(int s=0;s<i;s++) //for loop to control spacing
cout<<" ";
for(int p=0;p<num;p++) //for loop to control display of staric(*)
cout<<"*";
cout<<endl; //to end line
}


return 0;

}