How do you find the rest of the division?

4

I'm studying a book I found on the net, she asks for an exercise, but she did not teach how to do it in the previous pages, I tried and it did not work, thank you for your help.

  • Make a program that reads and stores any two integers in variables. Calculate and present the rest of the division of each other

    float num1, num2, media;
    
    printf("\n digite um valor: ");
    scanf("%f",&num1);
    printf("\n digite outro valor: ");
    scanf("%f",&num2);
    
    media = num1 % num2;
    
    printf("\n %f \n", media);
    
  • asked by anonymous 22.09.2015 / 15:26

    3 answers

    8

    In math it is only possible to find the rest when thinking of integers, so the type can not be float .

    #include <stdio.h>
    
    int main(void) {
        int num1, num2, resto;
        printf("\n digite um valor: ");
        scanf("%d",&num1);
        printf("\n digite outro valor: ");
        scanf("%d",&num2);
        resto = num1 % num2;
        printf("\n %d \n", resto);
        return 0;
    }
    

    See working on ideone .

    Use meaningful variable names. If you are calculating the rest, call it resto and not media .

        
    22.09.2015 / 15:32
    4

    The reason your code does not work has already been addressed, use variables of type int so that the division returns the rest, not a number with decimals. Another interesting way to solve this remainder problem of division is by using only math. This is the process we naturally do in a split account with remainder, we multiply the resultado by the divisor ( num1 ) and subtract it from the dividend ( num2 ), so we have resto :

    #include <stdio.h>
    
    int main(void) {
        int num1, num2, resultado, resto;
        printf("\n digite um valor: ");
        scanf("%d",&num1);
        printf("\n digite outro valor: ");
        scanf("%d",&num2);
        resultado = num1 / num2;
        resto = num1 - (resultado * num2);
        printf("\n %d \n", resto);
        return 0;
    }
    
        
    22.09.2015 / 16:08
    1
    #include <stdio.h>
    main() {
    int a,b;
    printf("Digite o valor do dividendo: ");
    scanf("%d",&a);
    printf("Digite o valor do divisor: ");
    scanf("%d",&b);
    while(a>=b)
    a=a-b;
    printf("\nResto: %d",a);
    getchar();
    }
    

    In this method, the user types the first value (dividend) and then types the second value (divisor). The two values enter into a while loop whose main function is for the value of the first value (dividend) to exit the loop when the dividend is smaller than the dividend.

    Example:

    If the user types 35 for the dividend and 4 for the divisor, we have:

    #include <stdio.h>
    main() {
    int a,b;
    printf("Digite o valor do dividendo: ");
    scanf("%d",&a);
    printf("Digite o valor do divisor: ");
    scanf("%d",&b);
    while(a>=b) {
    a=a-b;
    printf("\nValor do dividendo por enquanto = %d",a); //Linha acrescentada para mostar o laço
    }
    printf("\nResto: %d",a);
    getchar();
    }
    

        
    01.02.2016 / 04:29