+ 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
3 Respostas
+ 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
+ 2
thank u very much sir its very helpful.
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