Pyramid made out of numbers | Sololearn: Learn to code for FREE!
+ 1

Pyramid made out of numbers

Int st=0; int n=1; for(int i=1;i<=5;i++,st=0){ n=1; for(int space=1;space<=5-i;space++){ System.out.print(" "); } while(st != 2*i-1){ System.out.print(n); n++; st++; } System.out.println(" "); } } I made this code to make a pyramid out of numbers And the result was like this 1 1 2 3 1 2 3 4 5 How can i turn it to 1 2 3 2 3 4 5 4 3 Etc https://code.sololearn.com/cfdxDj37J7Ds/?ref=app

18th Sep 2020, 5:12 PM
Dareen Moughrabi
Dareen Moughrabi - avatar
7 Respuestas
+ 2
for(int i=1;i<=5;i++,sp=0) { q=i; b=true; for(int space=1;space<=5-i;space++) { System.out.print(" "); } while(sp !=2*i-1) { if (++sp>(2*i-1)/2) { b=false; } if(b) System.out.print(q++); else System.out.print(q--); } System.out.println(" "); } //Check this with your code for changes..
18th Sep 2020, 9:10 PM
Jayakrishna ūüáģūüá≥
19th Sep 2020, 3:13 AM
Vadivelan
+ 1
Instead of n=1, take n=i. And if(n>2*i-1) next decrease the value so to do that use a boolean value for either increamet or decrement... Try this way.. If not work, then also I think you may get the solution idea by this....
18th Sep 2020, 5:40 PM
Jayakrishna ūüáģūüá≥
+ 1
you are doing q-- in else so again chance to if statement comes true.. I said about, take a boolean value b: and set to false, when q<=2*i-1 becomes false.. Then for print statement use if(b) sop(q++) else sop(q--); If still need modifications, better to share the full code with saving in playground and share link here...
18th Sep 2020, 7:41 PM
Jayakrishna ūüáģūüá≥
0
//rows 5 for example int sp=0; int q; for(int i=1;i<=rows;i++,sp=0) { q=i; for(int space=1;space<=rows-i;space++) { System.out.print(" "); } while(sp !=2*i-1) { if (q<=2*i-1 ==true) { System.out.print(q); q++; sp++; } else { q=2*i-2; System.out.print(q); q--; sp++; } } System.out.println(" "); } } I almost got it But there is sth weird happing here 1 232 34543 4567656 567898789
18th Sep 2020, 6:48 PM
Dareen Moughrabi
Dareen Moughrabi - avatar
0
I have shared the full code , i still have some issues i believe its due to the fact that i didn't use boolean correctly
18th Sep 2020, 8:09 PM
Dareen Moughrabi
Dareen Moughrabi - avatar
0
Thx everyone i made the code and i understood which part i failed at .
19th Sep 2020, 4:08 AM
Dareen Moughrabi
Dareen Moughrabi - avatar