Search code examples
cif-statementerror-checking

How do I check if an int is evenly divisible by another int?


I'm trying to write a program that checks if an integer is divisible by another integer: 8 and 2 or 8 and 3. I'm using if statements. This is what I have so far:

if(int1 / 2)
{
    printf("2:yes \n");
}
else
{
    printf("2:no \n");
}

As long as the scanned number is below or equal to the divisor this prints 2:yes.

How do I check if an int is evenly divisible by another int?


Solution

  • Check the remainder using the % operator.

    if (int1 % 2 == 0) {
        printf("2: yes\n");
    } else {
        printf("2: no\n");
    }