0
Help with C++
Can someone help me with this problem, please? You need to make a countdown app. Given a number N as input, output numbers from N to 1 on separate lines. Also, when the current countdown number is a multiple of 5, the app should output "Beep". This is my code : #include <iostream> using namespace std; int main() { int n,i; cin >> n; //your code goes here for(i=1;i<=n;i++) { if(n%5==0) { cout<<n<<endl<<"Beep"<<endl; } else { cout<<n<<endl; } n--; } return 0; } When given input, my program outputs a countdown that stops halfway through. Can somebody explain why and what to do?
7 odpowiedzi
+ 4
You're counting i up and n down. And your end condition is i <=n.
+ 1
kishiberohan
Use while loop instead of for loop
while (n > 0)
+ 1
Try a while loop instead and it should be perfect!
0
A͢J The for loop does well, and saves a manual decrement. The OP just has to fix the conditions.
0
//Try this
#include<iostream>
using namespace std;
int main() {
int n,i;
cin >> n;
//your code goes here
for(i=n;i>=1;i--) {
if(i%5==0) {
cout<<i<<endl<<"Beep"<<endl;
} else {
cout<<i<<endl;
}
}
return 0;
}
0
Mohmad As3d Pls see my answer to A͢J
- 1
Why are you not close the open brashes