How to find the rest of the division?

Asked

Viewed 18,598 times

5

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

  1. Make a program that reads and stores in variables any two integers. Calculate and display the rest of the division of one by the 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);
    
  • You can accept an answer if it solved your problem. You can vote on every post on the site as well. Did any help you more? Need?

3 answers

9

In math, you can only find the rest when you think of whole numbers, so the guy can’t 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);
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

Use names of significant variables. If you are calculating the rest, call it resto and not of media.

4

The reason your code doesn’t work has already been covered, use variables like int so that the division returns the rest and not a number with decimals. Another interesting way to solve this problem from the rest of the division is by using only mathematics. This is the process that we do naturally in a division account with rest, we multiply the resultado by the divider(num1) and subtract that from the dividend(num2), so we have the 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;
}

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 enters the first value (dividend) then enters the second value (divisor). The two values enter a while loop whose main function is that the value of the first value (dividend) exits the loop when the dividend is less than the divisor.

Example:

If the user type 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();
}

Laço para o cálculo do resto sendo executado

  • Provide a little more detail in your reply!

Browser other questions tagged

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