+ 1

Print the number from 1 to entered number and from entered number to 1 using 1 loop,c++

Hello, I am stuck with one problem in c++, I have to enter a number and output needs to be from 1 to entered number and from entered number to 1, but using only one loop. I can't figure it out, what is the catch? Example: 1 2 3 4 4 3 2 1

7th Aug 2021, 11:33 AM
Dr. Alien
Dr. Alien - avatar
3 Answers
+ 4
Next time please link your attempt first. This will you help: https://code.sololearn.com/c6GLR0jiwCWk/?ref=app
7th Aug 2021, 12:03 PM
JaScript
JaScript - avatar
+ 2
Yes SIR!, Ty for help.
7th Aug 2021, 12:07 PM
Dr. Alien
Dr. Alien - avatar
+ 1
Maybe this might help #include <iostream> using namespace std; int main() { int num,key; cin >> num; key = num *2 +1; for (int i=1; i<key; ++i){ if(i <= num){ cout << i << endl; } else { cout << key -i << endl; } } return 0; }
7th Aug 2021, 12:35 PM
Rik Wittkopp
Rik Wittkopp - avatar