+ 1
So i was trying to do "the countdown" activity and my code doesnt work. Can anyone help me out?
#include <iostream> using namespace std; int main() { int n; int unimp=69420; cin >> n; while (n<unimp); { cout<<n--<<endl; } if (n=n*5) { cout<<"Beep"<<endl; } return 0; } Whats wrong with it?
6 Respostas
+ 1
#include <iostream>
using namespace std;
int main() {
int n;
int unimp=73936;
cin>> n;
while (n<unimp) //why you need this?
while (n>0)
{
cout<<n<< endl;
if (n%5==0)
cout<<"Beep"<< endl;
n-- ; // update after all print for next check
}
return 0;
}
// update n after printing.
+ 3
Remove semicolon after while( ) //; here
use == instead of = in if block, and you may need it in loop.. I think. It's out side of loop currently..
+ 2
Yes it worked! Thank you so much!!
+ 1
Used as a comparison, (n==n*5) would be false for all n except zero.
I think you are trying to determine whether n is a multiple of 5. In that case, use the modulo operator, %. Modulo calculates the remainder of a division. If the remainder is zero, then the divisor is an even multiple.
For example, 15/5 is 3 with remainder 0 (evenly divisible); whereas 19/5 is 3 with remainder 4 (not evenly divisible)
if (n%5==0)
{
...
+ 1
#include <iostream>
using namespace std; //you missed to add std here
int main() {
int n;
int unimp=73936;
cin>> n;
while (n<unimp) //why you need this?
while (n!=0)
{
cout<<n--<< endl;
if (n%5==0)
cout<<"Beep"<< endl;
}
return 0;
}
//Screecher check this code and if not work, then add your expected output.... hope it helps..
+ 1
You're welcome..