+ 5

How to print 1 2 3 4 5 6 7 8 9 10 9 8 7 6 5 4 3 2 1 in C++ by using only one loop?

23rd Nov 2017, 9:22 PM
Md. Asiful Hoque Prodhan
Md. Asiful Hoque Prodhan - avatar
13 Answers
+ 1
Here you can check my code for you https://code.sololearn.com/cM22opxG0CyH/?ref=app
23rd Nov 2017, 9:44 PM
Faisal Rahman
Faisal Rahman - avatar
+ 16
ok sorry I didn't paid attention! no reason to downvote like this.. be quiet bros! what about this? https://code.sololearn.com/cDA4szWAp6Xy/?ref=app
23rd Nov 2017, 10:16 PM
AZTECCO
AZTECCO - avatar
+ 13
for(int i = 1; i < 20; i++){ cout<<(i > 10) ? 20-i : i; }
23rd Nov 2017, 9:38 PM
Dapper Mink
Dapper Mink - avatar
+ 11
and also.. who downvotes should enlight us whit a good answer!
23rd Nov 2017, 10:22 PM
AZTECCO
AZTECCO - avatar
+ 11
@aztecco good, very good
23rd Nov 2017, 10:23 PM
Robert
Robert - avatar
+ 11
@aztecco it's probably a crazy man))
23rd Nov 2017, 10:25 PM
Robert
Robert - avatar
+ 11
You could always do it with no loops. https://code.sololearn.com/cJjsg7x4c6ST/?ref=app
24th Nov 2017, 4:12 AM
Louis
Louis - avatar
+ 10
thank you @Robert! but I'm not crazy.. or not at all lol
23rd Nov 2017, 10:32 PM
AZTECCO
AZTECCO - avatar
+ 9
@aztecco They require only one loop, but you wrote with two loops, try again
23rd Nov 2017, 9:59 PM
Robert
Robert - avatar
+ 4
#include <iostream> using namespace std; int main() { int x; int y = 9; for(int x = 1; x < 20; x++) { if (x <= 10) { cout << x << " "; } else { cout << y-- <<" " ; } } } // Output: 1 2 3 4 5 6 7 8 9 10 9 8 7 6 5 4 3 2 1
23rd Nov 2017, 10:16 PM
Fahem
Fahem - avatar
+ 3
a@a@a although is related you should make your own question. Btw mine in C i;f(){for(i=8;i--;)printf("%d %d ",9-i,i+2);} https://code.sololearn.com/c4CKbw0lM3gh/?ref=app
16th Mar 2019, 3:03 PM
AZTECCO
AZTECCO - avatar
+ 1
How toPrint FOR loop. Output: 2 9 3 8 4 7 5 6 6 5 7 4 8 3 9 2
16th Mar 2019, 10:01 AM
a@a@a
- 4
use nested For loops
23rd Nov 2017, 9:27 PM
Brandon
Brandon - avatar