What is the mistake for this timer code in C++? When I run the code, the "beep" gets displayed at the end. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

What is the mistake for this timer code in C++? When I run the code, the "beep" gets displayed at the end.

#include <iostream> using namespace std; int main() { int n; int b ; cin >> n; //your code goes here while (n>0) { cout << n << endl ; n-- ; } b = n % 5 ; if (b = 5) { cout << "Beep" ; } return 0; }

17th Apr 2021, 4:26 AM
Kruti
Kruti - avatar
7 Answers
+ 1
Hey Kruti You have to change just to lines of code. - place these two lines " n--; } " after the "beep" line.
18th Apr 2021, 9:15 AM
Satyam Keshri
Satyam Keshri - avatar
0
You can't use assignment operator in if conditions
17th Apr 2021, 4:40 AM
Atul [Inactive]
0
So what will be the correct code?
17th Apr 2021, 4:42 AM
Kruti
Kruti - avatar
0
#include <iostream> using namespace std; int main() { int n; cin >> n; while(n>=1) { if(n%5==0){ cout<<n <<"Beep"<<"\n";} else{ cout<<n; } n--; } return 0; }
17th Apr 2021, 4:43 AM
Atul [Inactive]
0
These are some necessary changes that you should bring in your code
17th Apr 2021, 4:43 AM
Atul [Inactive]
0
Ok thanks :) I got it now
17th Apr 2021, 4:55 AM
Kruti
Kruti - avatar
0
Kruti You can use do while loop here. First print number and then check if number is divisible by 5 if yes then print Beep. do { cout << n << endl; if (n % 5 == 0) cout << "Beep" << endl; n--; } while (n > 0);
17th Apr 2021, 5:31 AM
A͢J
A͢J - avatar