Hello guys, i have a silly question.
I'm doing an exercise where i need to countdown an user input value to 1 and need to print "Beep" every time a multiple of 5 is printed.
So the code it's very easy to write, but i don't know why it prints "beep" before the multiple of 5.
The code is:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
|
#include <iostream>
using namespace std;
int main()
{
int n;
cin >> n;
do {
cout << n << endl;
n--;
if (n % 5 == 0) {
cout << "Beep" << endl;
}
} while (n >= 1);
return 0;
}
| |
The output of this code is :
1 2 3 4 5 6 7
|
Beep
15
13
12
11
Beep
10
| |
and so on..
The print Beep must go after the 15, 10 and 5. Not before.
What am i doing wrong?
Last edited on
Let's say n = 11.
First, you print 11.
Then you decrement n.
n is now 10.
Then, you check if (n % 5 == 0), i.e. if n is divisible by 5.
You should be able to just move the n--; line below your if-block.
Also, please edit your post and add code formatting with
[code] {code here} [/code]
Last edited on
@Ganado
Thanks so much! i knew that was silly.. i have tried to move things around before posting but didnt work!!
Also, sorry about the post's format, i've edited it! thanks again!