+ 1
What is wrong with my code? Countdown. C++ tutorial
#include <iostream> using namespace std; int main() { int n; cin >>n; int i =n; while (n != 1){ if (i %5 ==0) { cout << "Beep"; cout << endl; } i--; cout << i; cout << endl; } return 0; }
2 Respuestas
+ 6
There were 2 problems with your code. The first is it went below negative numbers which is not needed and the second was the Beep was printed before the number when it should be printed afterwards
Fixed code:
#include <iostream>
using namespace std;
int main() {
int n;
cin >>n;
while (n>=1) {
cout<<n<<endl;
if (n %5 ==0) {
cout<<"Beep"<<endl;
}
n--;
}
return 0;
}
Hope this helps 👍
0
Thank you for your help❤