I Challenge can anyone make the program below using only 2 loops and ifelse statements ..? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

I Challenge can anyone make the program below using only 2 loops and ifelse statements ..?

1 121 12321 1234321 123454321 12345654321 123454321 1234321 12321 121 1

17th Oct 2016, 5:32 PM
Zoetic_Zeel
Zoetic_Zeel - avatar
5 Answers
+ 5
https://code.sololearn.com/cOaI43CDN1WV/#cpp Two for loops and one if-else :) I had to add a bit of space around the diamond so the Code Playground doesn't mess up the output. #include <cstdio> #include <cstdlib> #define P(c) putchar(c) #define A(x) abs(x) #define F(i) for(int i=-8;i<=8;i++) #define R(x) return 0; int main() { F (i) {F(j) {int a= A(i)+A(j); if(a>5)P(32) ;else P(54 -a);} P( 10);} R() ; }
17th Oct 2016, 8:26 PM
Schindlabua
Schindlabua - avatar
+ 3
Came up with my own solution as well: #include <iostream> #include <cmath> using namespace std; int main() { int x, i, j, m = 5; // m = Max for(i = -m; i <= m; i++) { x = 0; for(j = m; j >= -m; j--) cout << (abs(i)<=m-abs(j) ? (j<0?--x:++x) : 0); cout << "\n"; } return 0; } http://www.sololearn.com/app/cplusplus/playground/cvYHBj7gXPaG/
18th Oct 2016, 2:59 PM
Ted L
Ted L - avatar
+ 2
Great brother. I am just beginner to the c++. so I haven't thought this much. you are awesome.
17th Oct 2016, 9:19 PM
Zoetic_Zeel
Zoetic_Zeel - avatar
+ 1
what the hell.. did i just saw +schindlabua
14th Dec 2016, 5:32 PM
Faran Ahmed
Faran Ahmed - avatar
+ 1
it' s awesome you ask wish your future would be bright
27th Dec 2016, 3:07 PM
hariom