Pulse 0 Denunciar post Postado Junho 1, 2015 #include <stdio.h> #include <stdlib.h> int divisivel(float, float); int main () { float num1, num2; int div; puts("Digite dois numeros"); scanf("%f", &num1); do { scanf("%f", &num2); }while(num2 == 0); div = divisivel(num1, num2); if(div == 1) { puts("Sao divisiveis"); } else { puts("Nao sao divisiveis"); } } int divisivel(float n1, float n2) { if(n1%n2==0) { return 1; } else { return 0; } } Não consigo compilar o código quando defino os números como float, ao usar int ele compila e funciona normalmente. Qual o problema? Compartilhar este post Link para o post Compartilhar em outros sites
_Isis_ 202 Denunciar post Postado Junho 1, 2015 divisivel.c:33:10: error: invalid operands to binary % (have ‘float’ and ‘float’) if(n1%n2==0) O operador % só está definido para inteiros. #include <stdio.h> #include <math.h> int divisivel(float a, float b) { return (fmodf(a,b) < 10e-8); } int main (void) { float num1, num2; printf("Informe o primeiro número: "); scanf("%f", &num1); do { printf("Informe o segundo número: "); scanf("%f", &num2); if (!num2) puts("Não é possível realizar divisão por zero."); }while(!num2); if (divisivel(num1,num2)) { puts("Sao divisiveis"); } else { puts("Nao sao divisiveis"); } return 0; } Da documentação do fmodf: If the correct value would cause underflow, and is not representable, a range error may occur, and either 0.0 (if supported), or an implementation-defined value shall be returned. Ainda: comparações com ponto flutuante não são feitas com ==. Compartilhar este post Link para o post Compartilhar em outros sites