+ 1

For and while loop count down

Hi, I have tired the count down app problem: 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". I tried the for loop but "for" some reason the counter doesn't count down. it goes through the loop once and that is all. However when I used "while" loop it works fine, I am not sure why my "for" loop doesn't work as I expected it to work. #include <iostream> using namespace std; int main() { int n; cin >> n; for (int x = n; x >= 1; x=n-1 ) { cout << x << endl; if (x % 5 == 0) { cout << "Beep" << endl; } } /*while (n >=1) { cout << n <<endl; if (n % 5 == 0) { cout << "Beep" << endl; } n = n-1; }*/ return 0; }

4th Sep 2022, 8:36 AM
Humza Ali
Humza Ali - avatar
3 Respuestas
+ 3
Enough is for (int x = n; x >= 1; x-- )
4th Sep 2022, 8:40 AM
JaScript
JaScript - avatar
+ 1
Just miss out on the x Thank you! JaScript
5th Sep 2022, 4:24 PM
Humza Ali
Humza Ali - avatar
0
x =x-1 or x- -
5th Sep 2022, 4:26 PM
Humza Ali
Humza Ali - avatar