Determine that a number is divisible with another number in C

0

I hope you can help me with this:

#include <stdio.h>

int main() {
    int a, b, c;
    printf("ingrese 2 numeros: \n");
    scanf("%d%d", &a, &b);
    c=a%b;
    if ((b<a) && (c==0)) {
        printf("es divisible\n");
    }

    return 0;
}

If you can tell me what is wrong, it would help me a lot. Thanks!

    
asked by Josias99 04.04.2017 в 02:06
source

1 answer

3

the condition of if(b<a) && (c==0) could be simplified in the following way, if we want to know if a is divisible by b would be if(a % b == 0){ //Lo demas } as you would know, you only need to verify that the module ( % ) of a and b equals 0 that is to say the rest is exact, I leave you a proposal of solution:

#include <stdio.h>
int main() {
   int a, b;
   printf("Ingrese 2 numeros: \n");
   scanf("%d%d", &a, &b);
   if((a % b == 0) && (b!=0))
   {
      printf("%d es Divisible entre %d",a,b);
   }else{
      printf("%d NO es Divisible entre %d",b,a);
   }

   return 0;
   }

Anything or doubt, write to me, Regards.

  

Update 1:   Condition (b!=0) was added since the division between 0 does not have a defined value. Suggested change for: eferion

    
answered by 04.04.2017 / 03:19
source