mardi 18 mai 2021

how to make this number shape?

Write a program to display the below pattern with n rows, where n is in the range between 1 and 100. The variable n should be entered by the user. If the user input is between 1 and 100 then output the pyramid as given below, otherwise prompt the user to enter n again.

Here is the sample output: Enter the number of rows: 6

1

2 3

3 4 5

4 5 6 7

5 6 7 8 9

6 7 8 9 10 11

(enter image description here)

this is my code it shows a close answer but not correct.

 int num=1  ,  counter=1;
   cout << "Enter the number of rows: " ;
   cin>>num;

       for(int i=0;i<=num;i++)
       {
           for(int j=0;j<=i;j++)
           {
               cout<<counter<<" ";
               counter++;
           }
           cout<<endl;
       }

Aucun commentaire:

Enregistrer un commentaire