+ 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
7 odpowiedzi
+ 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..
+ 2
I did almost done.
https://code.sololearn.com/c8ib1D2r3N7P/?ref=app
+ 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....
+ 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...
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
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
0
Thx everyone i made the code and i understood which part i failed at .