A natural number is called cool if the sum of the digits which are in odd positions (starting to count on the right) is an even number. For instance, 2 and 679031 are cool, but 357199 and 607 are not.
Your task is to write a program that prints if a given number is cool or is not.
For example, what I understand the program does if n = 1
1. i = n and since i > 0 executes "for"
2. Take the remainder of 1/10 which is 0 and add it to c, which means c = 0 + 0 = 0
3. Divide 1/100 which gives i = 0
4. Since i = 0, ignore "for" and go to "if"
5. We got c = 0 so we execute the "if" and get: "1 IS COOL" (incorrect)
However, the program outputs: "1 IS NOT COOL" (correct answer) WHY???
Please I really need a good explanation for this, I do not understand.
1 2 3 4 5 6 7 8 9 10 11 12
|
#include <iostream>
using namespace std;
int main() {
int n;
int c = 0;
cin >> n;
for(int i = n; i > 0; i = i/100) c += i%10;
if(c%2 == 0 or c == 0) cout << n << " IS COOL" << endl;
else cout << n << " IS NOT COOL" << endl;
}
|