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; }
7 Answers
+ 1
Hey Kruti
You have to change just to lines of code.
- place these two lines
" n--;
} "
after the "beep" line.
0
You can't use assignment operator in if conditions
0
So what will be the correct code?
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;
}
0
These are some necessary changes that you should bring in your code
0
Ok thanks :) I got it now
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);