How does this program work?

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;
}
Last edited on
EDIT:
In your for-loop, c is set to i % 10, which means c is now 1, not 0, so the last statement executes and 1 IS NOT COOL

Modulus returns remainder, so 1 % 10 is 1

Your statement
2. Take the remainder of 1/10 which is 0 and add it to c, which means c = 0 + 0 = 0

is actually
Take the remainder of 1%10 which is 1 and add it to c, which means c = 0 +1 = 1


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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>
#include <cmath>

bool isCool( int n )
{
    // Find the total number of digits in n
    int numberOfDigits = std::log10( n ) + 1;
    int sum = 0;

    // Iterate numberOfDigits times
    for( int i = 1 ; i <= numberOfDigits ; i++ ) {
        // If i is odd, which means it's odd position in n
        if( i % 2 == 1 ) {
            // Add the digit at odd position to sum
            sum += n % 10;

            // Divide out the last digit
            n /= 10;
        }
        else {
            // Since this is an even position, simply divide out the last digit
            n /= 10;
        }
    }

    return sum % 2 == 0;
}

int main()
{
    int n;
    std::cin >> n;

    if( isCool(n) ) {
        std::cout << n << " IS COOL" << std::endl;
    }
    else {
        std::cout << n << " IS NOT COOL" << std::endl;
    }

    return 0;
}
Last edited on
Why 1%10 gives 1?
1/10 = 0'1 = 0 (because c++ truncates integers)
> because c++ truncates integers
Correct, C++ truncates the decimal places when dividing integers.

You need to understand that % and / are different

/ -> Division
% -> Modulo operation


1 divided by 10 is 0 with a remainder of 1

Therefore, 1/10 returns 0 and 1%10 returns 1

EDIT:
This is the long division
1
2
3
4
5
6
7
8

    0 
    _
10 | 1
   - 0
    _
     1
Last edited on
Modulo % gives remainder:
https://en.wikipedia.org/wiki/Modulo_operation

The 1 % 10 is same as 1 - 10 * (1/10).
You are correct, 1/10 (integers) is 0, therefore
1 - 10 * (1/10) becomes 1 - 10 * 0
which equals 1
Okay I understand now, thanks
Topic archived. No new replies allowed.