What is wrong with my code? Countdown. C++ tutorial | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

What is wrong with my code? Countdown. C++ tutorial

#include <iostream> using namespace std; int main() { int n; cin >>n; int i =n; while (n != 1){ if (i %5 ==0) { cout << "Beep"; cout << endl; } i--; cout << i; cout << endl; } return 0; }

5th Jan 2021, 2:54 AM
Salma Khaled
Salma Khaled - avatar
2 Answers
+ 6
There were 2 problems with your code. The first is it went below negative numbers which is not needed and the second was the Beep was printed before the number when it should be printed afterwards Fixed code: #include <iostream> using namespace std; int main() { int n; cin >>n; while (n>=1) { cout<<n<<endl; if (n %5 ==0) { cout<<"Beep"<<endl; } n--; } return 0; } Hope this helps 👍
5th Jan 2021, 3:14 AM
Pieter Edwards
Pieter Edwards - avatar
0
Thank you for your help❤
5th Jan 2021, 10:03 AM
Salma Khaled
Salma Khaled - avatar