- 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; }
7 Respostas
+ 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
+ 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.
+ 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;
}
0
Thank you for answering, yes in both cases the output is correct still it says it's not correct
0
The specific error with your code is you have wrote "Beep \n" when you need to write "Beep\n" with no space
0
Sakshi Wadhwa This was 5 months ago 😂
- 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;
}