- 2
This question is from C++ assignment please help
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".
10 Antworten
+ 15
#include <iostream>
using namespace std;
int main()
{
int n;
cin>>n;
for (int a = n; a >0; a--) {
cout << a << endl;
if(a%5==0)
{
cout<<"Beep"<<endl;
}
}
return 0;
}
+ 7
Hi!
Here is what I made to solve this:
#include <iostream>
using namespace std;
int main() {
int b;
cin>>b;
for(int a=b;a>=1;a--) {
cout<<a<<endl;
while (a%5==0) {
cout<<"Beep"<<endl<<a-1<<endl;
a=a-1;
}
}
return 0;
}
100% works.
I Hope it helped.
+ 4
#include <iostream>
using namespace std;
int main() {
int n;
cin >> n;
//your code goes here
do {
cout <<n <<endl;
if (n%5==0){
cout <<"Beep"<<endl;
}
n--;
}
while (n >0);
return 0;
0
Already tried , still asking for right ans
0
this works
#include <iostream>
using namespace std;
int main() {
int n;
cin >> n;
int a = n++;
for (a=n; a--;)
if (a>0)
{cout << a << endl;
if (a%5==0 && a>0) {cout << "Beep" << endl;
}
}
return 0;
}
0
Can we do it using while loop
0
This is indeed possible with the while loop, this is the simplest I could get using a while loop with everything that has been taught up to this point. Added explanations just incase anyone later on still needs help, hope it's useful :)
/* Countdown with a while loop */
int n; // this will be the user input
cin >> n; // this takes the user input
while (n >= 1) { /*the while loop starts here and checks if it is bigger or equal to 1* and if it is then it executes loop */
cout << n << endl; // this prints the current number and ends line
if (n % 5 == 0) { /*in the condition brackets, the modulus operator is used to check if n divided by 5 has a remainder, if it does not, then it is indeed a multiple of 5 */
cout << "Beep" << endl; // if it is a multiple of 5, then it will print "Beep", if not, it will just skip the whole if operator
}
n = n - 1; // this just subtracts 1 from the current number to countdown by 1
}
0
#include <iostream>
using namespace std;
int main()
{
int n,i;
cin>>n;
if(n<=4){
while(n<=0)
{
cout<<"n";
n--;
}
}
for(i=n;i>0;i--)
{
cout<<i;
if(i%5){
cout<<"\n";
}
else{
cout<<"\nBeep\n";
}
}
return 0;
}
0
#include <iostream>
using namespace std;
int main() {
int n;
cin >> n;
for(;n>0;n--){
cout<<n<<endl;
if(n%5==0)
{cout<<"Beep"<<endl;}
}
return 0;
}
0
#include <iostream>
using namespace std;
int main() {
int n;
cin >> n;
for (int a=n;a>0;a--){
cout<<a<<endl;
while(a%5==0)
{
cout<<"beep"<<endl<<a-1<<endl;
a=a-1;
}
}
return 0;
}
Why it is not right? Every output is right…