Division error by zero

Asked

Viewed 825 times

0

I am a beginner in programming and I have to assemble a code in which I put two values and the program gives me the result in sum, subtraction, multiplication and division, but when dividing a number by zero an error message appears. I was able to get an error message to appear if it is divided by zero, but I can’t do any other kind of division than zero. If anyone can enlighten me because I understand little yet of programming.

That is the code:

#include <stdio.h>
#include <stdlib.h>

int main()
{
  int num1, num2, soma, subtracao, multiplicacao, divisao;

  printf("Digite o primeiro numero:");
  scanf_s("%d", &num1);

  printf("Digite o segundo numero:");
  scanf_s("%d", &num2);

  soma = num1 + num2;
  subtracao = num1 - num2;
  multiplicacao = num1 * num2;
  divisao = num1 / num2;

  printf("A soma = %d\n", soma);
  printf("\n");
  printf("A subtracao = %d\n", subtracao);
  printf("\n");
  printf("A multiplicacao = %d\n", multiplicacao);
  printf("\n");
  printf("A divisao = %d\n", divisao);

  if (num2 != 0)
    divisao = num1 / num2;
  else
    printf("Invalido, nao pode ser divido por 0");

  printf("\n");

  system("pause");
  return 0;
}
  • This ain’t C#, man. It most probably ain’t got nothing to do with dynamic programming either.

2 answers

1

Its logic to verify if the variable num2 is equal to zero is right, but it is being executed after the division, its code should look something like this:

int num1, num2, soma, subtracao, multiplicacao, divisao;

printf("Digite o primeiro numero:");
scanf_s("%d", &num1);

printf("Digite o segundo numero:");
scanf_s("%d", &num2);



soma = num1 + num2;
subtracao = num1 - num2;
multiplicacao = num1 * num2;

divisao = -9999; //um valor estranho pra ser interpretado como um erro
if (num2 != 0)
    divisao = num1 / num2;
else
    printf("Invalido, nao pode ser divido por 0");

printf("A soma = %d\n", soma);
printf("\n");
printf("A subtracao = %d\n", subtracao);
printf("\n");
printf("A multiplicacao = %d\n", multiplicacao);
printf("\n");
printf("A divisao = %d\n", divisao);

system("pause");
return 0;

Remember, always check for anything that might go wrong before of the execution.

1

Print the split output only if such an operation is possible.

#include <stdio.h>

int main() {
    int num1, num2, soma, subtracao, multiplicacao, divisao;

    printf("Digite o primeiro numero:");
    scanf("%d", &num1);

    printf("Digite o segundo numero:");
    scanf("%d", &num2);

    soma = num1 + num2;
    subtracao = num1 - num2;
    multiplicacao = num1 * num2;

    printf("A soma = %d\n\n", soma);
    printf("A subtracao = %d\n\n", subtracao);
    printf("A multiplicacao = %d\n\n", multiplicacao);

    if (num2 != 0) {
        divisao = num1 / num2;
        printf("A divisao = %d\n\n", divisao);
    }
    else
        printf("Invalido, nao pode ser divido por 0\n\n");

    system("pause");
    return 0;
}
  • I managed to do it this way, it worked the code thanks who helped xD

Browser other questions tagged

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