+ 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
3 odpowiedzi
+ 4
Next time please link your attempt first. This will you help:
https://code.sololearn.com/c6GLR0jiwCWk/?ref=app
+ 2
Yes SIR!, Ty for help.
+ 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;
}