- 3
22 End of module project
How can i make the ‘beep’ below the 5 and the 10 rather than above them and remove the ‘beep’ below 1?? Thats my code: #include <iostream> using namespace std; int main() { int n; cin>>n; //your code goes here n=12; while(n>=1){ cout<<n<<endl; n=n-1; if(n%5==0){ cout<<"Beep"<<endl; } } }
23 Antworten
+ 2
I already explained, but I will try to do it again with other words...
n store the user input by using 'cin'
the 'for' loop takes 3 parts inside parenthesis after the 'for' keyword:
1) initialization (executed once at loop start), usually initialization of counter variable
2) condition (checked at start of each loop iteration)
3) part executed at end of each iteration (usually in/decrementation of variable counter)
you 'for' loop first part set n to 12, wich is the example case provided, but you need to use the value assigned to n by 'cin'... so you just need to leave empty the initialization part ;)
+ 1
This will help you:
https://code.sololearn.com/c0k7xG5fMG5o/?ref=app
+ 1
your code only handle the example test case... you must use n given by input:
for (; n>=1;n--){
...
don't forget the first semi colon ;)
+ 1
your code get n from user input, but redefine n to 12 (the example test case) in the first part of the for loop parenthesis...
you must leave this part empty, as n is still initialized with the right value ^^
+ 1
Matt Burtz that's not your thread: open your own question thread, and request for help in it, by providing your code attempt (better to share the link to a code playground)
0
you should iterate in right order: from 1 to n, not from n to 1 ;)
0
made some changes and it gives the right order and the beep where they should be, but still it doesnt indicate that my answer is right.
#include <iostream>
using namespace std;
int main() {
int n;
cin>>n;
for(int n=12;n>=1;n--){
if(n% 5 == 0) {
cout << n<< endl;
cout << "Beep" << endl;
}
else
cout << n<< endl;
}
return 0;
}
0
copy the task description/requirement here to refresh our memory ;)
0
still wouldnt work
0
copy the task description/requirement here to refresh our memory ;)
0
visph 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".
Sample Input:
12
Sample Output:
12
11
10
Beep
9
8
7
6
5
Beep
4
3
2
1
0
visph (sorry)huh..?
0
still bro..
0
???
0
still wouldnt work although i think i dont think i fully understood the last thing u said
0
just replace your 'for' line with the one I given previously, without missing the first semi colon (;)
0
visph it worked thank you, i just need explaination to what was wrong and what changed
0
ooooohh,thanks budd!! much appreciation!!
0
What does that first semi colon do?
0
Matt Burtz for loop parenthesis must provide 3 parts, semi colon separated, even if you don't need to do something inside... first part is initialization (done only once at start of loop), so if the variable is already initialized before, you have no need of content in it, but you must mark the end of this empty part with a semi colon ;)