Any ways to simplify this c++ code? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Any ways to simplify this c++ code?

#include<iostream> using namespace std; int main() { int n(1),r; for(int i=1;i<=5;i++) { if(i<=3) {while(n<=i*i) { cout<<n<<" "; n+=i; } n=i+1; } else {n=6-i; while(n<=(6-i)*(6-i)) { cout<<n<<" "; n+=(6-i); } n=6-i; n--; } cout<<endl; } return 0; } This gives output 1 2 4 3 6 9 2 4 1

5th Jan 2021, 5:12 AM
Dex
Dex - avatar
3 Answers
+ 6
There are probably many ways to produce this type of output. Here is my solution. Hopefully you find this helpful. https://code.sololearn.com/cII4df6Wu0Pb/?ref=app
5th Jan 2021, 5:38 AM
Elizabeth Kelly
Elizabeth Kelly - avatar
+ 2
thank u very much sir its very helpful.
5th Jan 2021, 5:39 AM
Dex
Dex - avatar
0
This is another approach that has a mathematical bent. It computes a triangle wave using the abs() function. https://code.sololearn.com/cVn2VOS92ZGh/?ref=app
5th Jan 2021, 7:42 AM
Brian
Brian - avatar