+ 1

What is wrong with this code?

If user inputs 5 then the program should print : 5 4 3 2 1 0 I tried to do this using while loop in c++ but the code is not printing anything, this is the code: #include <iostream> using namespace std; int main() { int n; cin>>n; while(n<=0){ cout<<n<<"\n"; n--; } } Please Help.

12th Nov 2020, 6:28 AM
Aditya
Aditya - avatar
4 ответов
+ 5
Hi! Just change <= for >= If you input 5, 5 is not less or equal to 0, so you while will never be executed
12th Nov 2020, 6:42 AM
Mariano Fregosi
Mariano Fregosi - avatar
+ 3
Oh yes, that happens when you are handling a lot of associative arrays in php, one usually gets confused 🙄 Anyways a compile run error fix it, so calm down you "human compiler" 🤗
12th Nov 2020, 7:03 PM
Mariano Fregosi
Mariano Fregosi - avatar
+ 2
Incredible #include <iostream> using namespace std; int main() { int n; cin>>n; while(n>=0){ cout<<n<<"\n"; n--; } return 0; }
12th Nov 2020, 7:06 AM
BroFar
BroFar - avatar