Help with C++ | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Help with C++

Can someone help me with this problem, please? 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". This is my code : #include <iostream> using namespace std; int main() { int n,i; cin >> n; //your code goes here for(i=1;i<=n;i++) { if(n%5==0) { cout<<n<<endl<<"Beep"<<endl; } else { cout<<n<<endl; } n--; } return 0; } When given input, my program outputs a countdown that stops halfway through. Can somebody explain why and what to do?

29th Apr 2022, 3:49 PM
Florian Catalin
7 Answers
+ 4
You're counting i up and n down. And your end condition is i <=n.
29th Apr 2022, 4:06 PM
Simon Sauter
Simon Sauter - avatar
+ 1
kishiberohan Use while loop instead of for loop while (n > 0)
29th Apr 2022, 4:18 PM
A͢J
A͢J - avatar
+ 1
Try a while loop instead and it should be perfect!
23rd Feb 2023, 6:57 AM
Mohamad Asaad
Mohamad Asaad - avatar
0
A͢J The for loop does well, and saves a manual decrement. The OP just has to fix the conditions.
29th Apr 2022, 11:43 PM
Emerson Prado
Emerson Prado - avatar
0
//Try this #include<iostream> using namespace std; int main() { int n,i; cin >> n; //your code goes here for(i=n;i>=1;i--) { if(i%5==0) { cout<<i<<endl<<"Beep"<<endl; } else { cout<<i<<endl; } } return 0; }
1st May 2022, 8:15 AM
Aqib Raza
0
Mohmad As3d Pls see my answer to A͢J
23rd Feb 2023, 10:41 AM
Emerson Prado
Emerson Prado - avatar
- 1
Why are you not close the open brashes
29th May 2022, 8:04 PM
ARYAN RAJ