I can’t get this program to work. It always says the numbers are divisible

Asked

Viewed 70 times

1

#include <stdio.h>
#include "funcao.h"

int main () {
    int a, b;
    printf("Escreva os valores de a e b   ");
    scanf("%d", &a);
    scanf("%d", &b);

    if (EDivisivel (a,b))
        printf("sao divisiveis");
    else
        printf("nao sao divisiveis");
        return (0);
}

/* function is down here: */

#include <stdio.h>
int EDivisivel (int a, int b) {
    if (a%b)
        return 1;
    else
        return 0;
}

From that, I never get certain results from nondivisible numbers

1 answer

1


For a number to be considered divisible by another rest of the division (%) between them has to be equal to 0. Ex: 6 / 3 = 2 and rest = 0.

When you do if (a%b), this condition will be true if the rest of the division is 1. For 1 -> True and 0 -> False.

So, the correct thing would be to check like this: if (a%b == 0).

  • Thank you! It worked right here! Thank you very much

Browser other questions tagged

You are not signed in. Login or sign up in order to post.