0

How to make it appear every multiple of 5 "beep"

#include <iostream> using namespace std; int main() { int n; cin >> n; for(int x=n; x>0; x--) { cout<<x<<endl; if(x %5 == 0){ cout<<"beep"<<endl; } } }

11th Dec 2020, 12:17 PM
Lidandi Biki
Lidandi Biki - avatar
5 Respuestas
+ 1
Lidandi Biki The problem is, the "cout << n << endl;" is placed before the condition "if (x % 5 == 0)", hence, the number with the multiple of 5 will print first, before the word "beep" Solution: Place the "cout << x << endl;" to an else statement so that if a number is multiple of 5, it will print the "beep" without the number. https://code.sololearn.com/cJI74MyH5o0X/?ref=app
11th Dec 2020, 12:23 PM
noteve
noteve - avatar
+ 1
nicko's code is doesn't work, look for my code #include <iostream> using namespace std; int main() { int n; cin >> n; for (int i=n;i>0;i--) { cout << i; if(i % 5 == 0) cout << "Beep"; } return 0; }
11th Dec 2020, 12:27 PM
Иван Чикyнов
Иван Чикyнов - avatar
+ 1
Иван Чикyнов Sorry, but I dont think thats the right code, it is just the same with the previous one, you just removed the "endl".
11th Dec 2020, 12:32 PM
noteve
noteve - avatar
+ 1
ok
11th Dec 2020, 12:32 PM
Иван Чикyнов
Иван Чикyнов - avatar
0
Thanks everyone, this code is work
11th Dec 2020, 12:30 PM
Lidandi Biki
Lidandi Biki - avatar