Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

create a program that will ask the user enter a character to be output and the m

ID: 3564756 • Letter: C

Question

create a program that will ask the user enter a character to be output and the maximum number of times the character will appear on a line. Then output a pattern in which the entered pattern appears once on the first line, twice on the second line, three times on the third line and so on until the character appears the maximum number of times. Then on the next lines the number of times the character appears should decrease by one until it is no longer output.

For example if the user entered Q for the character and 4 for the maximum number of times the output should look like

Q

QQ

QQQ

QQQQ

QQQ

QQ

Q

Your program should use a while loop to confirm the user entered a valid entry for the maximum number of time the character should be output is at least one. Then use nested loops to output the pattern of the characters

Explanation / Answer

#include <iostream>
using namespace std;

int main()
{
   char a ;
   int m;
   while(cin>>a)
   {
       if(((int)a>=65 && (int)a<=90)|| ((int)a>=97 && (int)a<=122))
       {
           cin>>m;
           for(int i=1;i<=m;i++)
           {
               for(int j=1;j<=i;j++)
               {
                   cout<<a;
               }
               cout<<endl;
           }
           for(int i=m-1;i>=1;i--)
           {
               for(int j=1;j<=i;j++)
               {
                   cout<<a;
               }
               cout<<endl;
           }
          
       }
       else
       {
           cout<<"invalid input"<<endl;
           cout<<"enter a valid character"<<endl;
       }
   }
   return 0;
}