0

Print this ....1,-4,7,-10....using for loop

for loop

11th Sep 2017, 7:55 AM
rishabh Singh
rishabh Singh - avatar
5 Réponses
+ 12
for (int i = 1, j = 3; i < n; i = -i, j = -j, i += j) { std::cout << i << " "; }
11th Sep 2017, 10:21 AM
Hatsy Rei
Hatsy Rei - avatar
+ 9
for(int i = 0; i < n; ++i) cout << (i&1 ? -3*i - 1 : 3*i + 1) <<endl; Edited with swim correction
11th Sep 2017, 9:31 AM
Baptiste E. Prunier
Baptiste E. Prunier - avatar
+ 6
for(int i = 1; i < 11; i = i > 0 ? -(i+3) : -i+3) { cout << i << endl; }
11th Sep 2017, 8:49 AM
ChaoticDawg
ChaoticDawg - avatar
+ 3
for(int x=1,j=1;j<=4;j++){ (j%2==0)?(x=x*-1):(x=x); cout << x << " "; (j%2==0)?(x=x*-1):(x=x); x+=3; } I decided to do them the long way :)
11th Sep 2017, 11:54 PM
HJ🐓TDM