c++ | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

c++

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

23rd Sep 2021, 3:25 PM
Mando
Mando - avatar
8 Answers
+ 3
#include <iostream> using namespace std; int main() { int n; cin >> n; while(n>=1){ if(n%5==0){ cout<<n<<endl; cout <<"Beep" << endl; } else{ cout<<n<<endl; } n--; } return 0; } Don't code according to test case. 12 is just a sample test case. There could b any test case. And to check whether a number is a multiple of another number, take its modulus with the number you want to check. Is it equal to 0 or not.
23rd Sep 2021, 3:39 PM
HK Lite
HK Lite - avatar
+ 3
HK Lite No need else part here, you can simply do: cout << n << endl; if (n % 5 == 0) cout << "Beep" << endl;
23rd Sep 2021, 5:47 PM
A͢J
A͢J - avatar
+ 1
#include <iostream> using namespace std; int main() { int r; int n; int number= 0; cin >> n; if ( r < 12) { cout << r << endl; r-=1; r--; } number = 0/5; if (number/5) cout << "beep" << endl; return 0; }
23rd Sep 2021, 3:26 PM
Mando
Mando - avatar
+ 1
And this
23rd Sep 2021, 3:32 PM
Mando
Mando - avatar
+ 1
#include <iostream> using namespace std; int main() { int n; int number= 0; cin >> n; if ( number/5 ) cout << "beep" << endl; while (number > 12) { number-=12; n--; } return 0; }
23rd Sep 2021, 3:32 PM
Mando
Mando - avatar
23rd Sep 2021, 3:37 PM
Simon Sauter
Simon Sauter - avatar
+ 1
HK Lite your code doesnt inculde 15 it replaces it with beep Its suppose to go 15 beep 14..
23rd Sep 2021, 3:45 PM
Mando
Mando - avatar
+ 1
HK Lite yea true
23rd Sep 2021, 3:48 PM
Mando
Mando - avatar