What is wrong with my code for C++ CountDown | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
- 1

What is wrong with my code for C++ CountDown

#include <iostream> using namespace std; int main() { int n,a; cin >> n; while(n>0) { cout<<n<<endl; a=n%5; if(a==0) { cout<<"Beep \n"; } n--; } return 0; }

27th Dec 2020, 7:18 PM
Muzaffar Najib
Muzaffar Najib - avatar
7 Answers
+ 1
Thank you both of you. Pieter you are correct, I changed from \n to endl. And it worked. But still there is no syntex error if we use it in CPP. Anyhow really appreciate
27th Dec 2020, 7:36 PM
Muzaffar Najib
Muzaffar Najib - avatar
+ 1
Well, it's not actually wrong but the question you're trying to solve doesn't want a space after Beep. We can't see any difference but the program can see that your output isn't same as the expected answer so it says your program doesn't work correctly.
27th Dec 2020, 8:11 PM
Mert Yazıcı
Mert Yazıcı - avatar
+ 1
#include <iostream> using namespace std; int main() { int n; cin >> n; for(int i = n; i >= 1; i--) { cout << i << endl; if(i % 5 == 0) { cout << "Beep" << endl; } } return 0; }
1st Jun 2021, 4:43 PM
Sakshi Wadhwa
0
Thank you for answering, yes in both cases the output is correct still it says it's not correct
27th Dec 2020, 7:23 PM
Muzaffar Najib
Muzaffar Najib - avatar
0
The specific error with your code is you have wrote "Beep \n" when you need to write "Beep\n" with no space
27th Dec 2020, 7:32 PM
Pieter Edwards
Pieter Edwards - avatar
0
Sakshi Wadhwa This was 5 months ago 😂
1st Jun 2021, 9:46 PM
Pieter Edwards
Pieter Edwards - avatar
- 1
For future reference, this works as well. #include <iostream> using namespace std; int main() { int n{}; cin >> n; for (n; n >= 1; --n) { if (n % 5 == 0) { cout << n << "\n" << "Beep" << endl; } else { cout << n << endl; } } return 0; }
17th Jul 2021, 8:52 PM
Bruce Duncan
Bruce Duncan - avatar