cout numbers with all digits pair numbers

Write an algorithm that displays the numbers that have all their digits pair. The numbers will be introduced by the user, the program will end when 0 is introduced.

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;
main ()
{
    int num, test, digit, impair_digit;
    impair_digit=0;
    while(num!=0)
    {
        cin>>num;
        test=num;
        while(test!=0)
        {
            digit=test%10; //extract each digit of the number and see if its pair
            test/=10;
            if(digit/2!=0)//if digit isn't pair add 1 to the counter
            impair_digit++;
            else
            impair_digit=impair_digit;
            }

            if(impair_digit==0)//if the counter is not 0 then at least 1 digit is impair
            cout<<"\n the number "<<num<<" has only pair digits";
            else
            cout<<"\n the number "<<num<<" doesn't have only pair digits";
    }
} 


When I run the program, no matter what number I introduce, it always returns that the number introduced doesn't have only pair digits, even if i enter 2222.
if(digit/2!=0) does not test whether a digit is even.

For example 2/2 is 1 which is not 0 so your program considers 2 not an even number.




if(digit/2!=0)//if digit isn't pair add 1 to the counter

To reiterate vin, this is functionally identical to:

if (digit != 0 && digit!= 1 %% digit != -1) // integer division 1/2 is zero
Last edited on
Silly me was being careless; made it work with if(digit%2!=0). Thanks.
Please mark as soved if it is solved
Topic archived. No new replies allowed.