Reverse my code, help

I have the correct code, but I can't seem to reverse the order of how it appears.

#include<iostream>
#include<cmath>

using namespace std;

int main(){

int numRows = 0; // Declaring variable number of rows and counter variables 'i' and 'k'
int i = 0;
int k = 0;

cout<<"Enter number of rows: "; //Taking user input for number of rows
cin>>numRows;

while(i <= numRows){ // start of outer while loop
k=1; //set counter variable k to start at 1
i++; // increments counter i and adds 1 each time
while(k <= i){
cout<< k%10;
k++;
}
cout<<endl;


}

return(0);



}


the output for numRows=12 is:
1
12
123
1234
12345
123456
1234567
12345678
123456780
1234567801
12345678012
123456780123
1234567801234

I need it to be
1234567801234
123456780123
12345678012
etc...
Well, you're running a loop that takes k from 0 to the number of the row that you're currently on. If you go backwards, you don't want k to go to the number of the row, but (total number of rows) - (number of row). If you simply change your i variable this should work.
hello dear !

read the comments in code.
here is the reversed code for you ! enjoyyyyyyyyyyyyyyyy it !

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include<iostream>
#include<cmath>

using namespace std;

int main(){

int numRows; 
int save;
cout<<"Enter number of rows: ";
cin>>numRows;
save=numRows;

for(int i=0;i<numRows;i++){

    for(int j=1;j<=save;j++){ // your mistake was here!!!
                              // when you want print outputs of (j%10) in each line for several times
                              // you must design an inner loop for it
        cout<<j%10;
    }
// when the inner loop finished,
// you just decrease the limitation of inner loop(save) by 1 at each time.

    save--;
    cout<<endl;

}
    return(0);
}


is it useful ??
Last edited on
Topic archived. No new replies allowed.