- 1
Count down project
Count down project 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 There's a code below this question please I need an explanation for it
6 Respostas
0
#include <iostream>
using namespace std;
int main() {
int n;
cin >> n;
if (n<0){
cout << "Time cannot be negative"<< endl;
cin >> n;
}
// minimum countdown value is 1. hence, 0 is not an acceptable output so condition to use is n > 0 or you can use n>=1.
// for each countdown, you decrease the previous value by 1. written as n-- .
for (n ; n> 0 ; n--) {
// condition for beep (Number must be divisible by 5)
// challenge requires that you display the value which is divisible by 5 before displaying the Beep. So we display both outputs
if ( n%5 == 0 ){
cout << n << endl;
cout << "Beep" << endl;
}
else {
cout << n << endl;
}
}
return 0;
}
0
There are some comments in the code that explain it. Which part precisely do you struggle with?
0
For each count down u reduce the number by one? Why?
0
And what numbers are divisible by 5 in this code?
0
And what does multiple of 5 mean?
0
n-- because it counts downwards, for example: 9, 8, 7, ...
A number is divisible by 5 if (n % 5) == 0
% is the modulo operator, it gives us the remainder of a division. If the remainder equals 0 we know there is remainder, hence it is fully divisible by 5