Why does this not give me this 15 beep 14 13 12 11 10 beep 9 8 7 6 5 beep 4 3 2 1 (imagine each one of them is in ligne) | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Why does this not give me this 15 beep 14 13 12 11 10 beep 9 8 7 6 5 beep 4 3 2 1 (imagine each one of them is in ligne)

#include <iostream> using namespace std; int main() { int n; cin >> n; //your code goes here while (n>=1){ cout << n <<endl; n--; if ( n>= 1 && n% 5 == 0){ cout<<"beep"<<endl; } } return 0; }

14th Nov 2022, 5:24 PM
haythem DRIDI
haythem DRIDI - avatar
2 Answers
+ 1
while ( n > 0) { cout << n << endl; if (n % 5 == 0) { cout << "Beep" << endl; } n--; } return 0; }
14th Nov 2022, 9:00 PM
**🇦🇪|🇦🇪**
**🇦🇪|🇦🇪** - avatar
+ 5
Suppose we enter 15. n is printed before it is evaluated. Then it is decreased by 1. So by the time your if-statement is reached, n is 14.
14th Nov 2022, 5:34 PM
Lisa
Lisa - avatar