0

Slight help please

Write a program to display the below pattern with rows, where is in the range between 1 and 100. The variable 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 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 ........... What to do to make it in a correct form My code is #include <iostream> using namespace std; int main(){ int n; int num=1; cout << " Enter the number of rows:"; cin >> n; for( int row =1 ; row <= n; row++){ int x=row; for (int num= 1 ; num <= row ; ++num ){ cout << num <<" " ; ++x; } cout << endl; } return 0;} My output is 1 1 2 1 2 3 N so on

18th Dec 2020, 3:00 AM
Seraphina
Seraphina - avatar
2 ответов
+ 5
Seraphina You need to print x not num because num will always start from 1 after every iteration of loop 1 See this. #include <iostream> using namespace std; int main(){ int n; //int num = 1; cout << " Enter the number of rows:" << endl; cin >> n; for( int row =1 ; row <= n; row++){ int x=row; for (int num= 1 ; num <= row ; ++num ){ cout << x <<" " ; x++; } cout << endl; } return 0; }
18th Dec 2020, 3:10 AM
A͢J
A͢J - avatar
+ 1
Oh thankkk uuuu
18th Dec 2020, 3:30 AM
Seraphina
Seraphina - avatar