decimal division result

Asked

Viewed 65 times

-1

I searched here on the forum and I saw similar questions, but I couldn’t solve the problem.

Exercise: "Implement a calculator. The program must ask the user for 3 numbers: 'a', 'b' and 'operation'. If'operation' is 1, you must print the sum of 'a + b'. If it is 2, subtraction. if it is 3, multiplication. If it is 4, division."

I have already declared 'a', 'b' and 'result' all as double, as float, as int, and never quite right.

The code is like this:

#include <stdio.h>
#include <stdlib.h>
int main(){
    int a;
    int b;
    int operacao;
    double resultado;
    printf("ESCOLHA DOIS NUMEROS E A OPERACAO QUE SERA FEITA ENTRE 
            ELES\n");
    printf("ESCOLHA O NUMERO A\n");
    scanf("%d", &a);
    printf("ESCOLHA O NUMERO B\n");
    scanf("%d", &b);
    printf("ESCOLHA A OPERACAO\n");
    printf(" 1 = SOMA\n 2 = SUBTRACAO\n 3 = MULTIPLICACAO\n 4 = 
           DIVISAO\n");
    scanf("%d", &operacao);
    if(operacao == 1){
        resultado = a + b;
        printf("\n%d + %d = %.2f\n", a, b, resultado);
    }else{
        if(operacao == 2){
            resultado = a - b;
            printf("\n%d - %d = %.2f\n", a, b, resultado);
        }
        if(operacao == 3){
            resultado = a * b;
            printf("\n%d X %d = %.2f\n", a, b, resultado);
        }
        if(operacao == 4){
            resultado = (double)(a / b);
            printf("\n%d / %d = %.2f\n", a, b, resultado);
 }  }  }

In this format it works perfectly when the result is an integer, but when the result is decimal, zeros only appear after the comma.

When I declared variables as double or float the program only printed zeros...

I’m sorry if that question is too recurring, but I really couldn’t, and I’ve looked at all the answers that could help me.

1 answer

1

You need to convert the numbers to double before division, in your example you divide the integers and only then do the conversion:

resultado = (double)(a / b);

To fix and have a number with decimals, just convert one of the numbers before the division, example:

resultado = (double)a / b;

With this your division will already have a result double, containing decimals.


A point of your code that can be improved is the operation check if it is different from 1, you do other three ifs.

Here we can use the else if:

if(operacao == 1){
    resultado = a + b;
    printf("\n%d + %d = %.2f\n", a, b, resultado);
}
else if(operacao == 2){
    resultado = a - b;
    printf("\n%d - %d = %.2f\n", a, b, resultado);
}
else if(operacao == 3){
    resultado = a * b;
    printf("\n%d X %d = %.2f\n", a, b, resultado);
}
else if(operacao == 4){
    resultado = (double)a / b;
    printf("\n%d / %d = %.2f\n", a, b, resultado);
}

Or a switch:

switch(operacao) {
  case 1: {
      resultado = a + b;
      printf("\n%d + %d = %.2f\n", a, b, resultado);
      break;
  }
  case 2: {
      resultado = a - b;
      printf("\n%d - %d = %.2f\n", a, b, resultado);
      break;
  }
  case 3: {
      resultado = a * b;
      printf("\n%d X %d = %.2f\n", a, b, resultado);
      break;
  }
  case 4: {
      resultado = (double)a / b;
      printf("\n%d / %d = %.2f\n", a, b, resultado);
      break;
  }
}

Your complete code will look like this:

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

int main(){
    int a;
    int b;
    int operacao;
    double resultado;

    printf("ESCOLHA DOIS NUMEROS E A OPERACAO QUE SERA FEITA ENTRE ELES\n");
    printf("ESCOLHA O NUMERO A\n");
    scanf("%d", &a);
    printf("ESCOLHA O NUMERO B\n");
    scanf("%d", &b);
    printf("ESCOLHA A OPERACAO\n");
    printf(" 1 = SOMA\n 2 = SUBTRACAO\n 3 = MULTIPLICACAO\n 4 = DIVISAO\n");
    scanf("%d", &operacao);

    if(operacao == 1){
        resultado = a + b;
        printf("\n%d + %d = %.2f\n", a, b, resultado);
    }
    else if(operacao == 2){
        resultado = a - b;
        printf("\n%d - %d = %.2f\n", a, b, resultado);
    }
    else if(operacao == 3){
        resultado = a * b;
        printf("\n%d X %d = %.2f\n", a, b, resultado);
    }
    else if(operacao == 4){
        resultado = (double)a / b;
        printf("\n%d / %d = %.2f\n", a, b, resultado);
    }

    return 0;
}

See online: https://repl.it/@Dadinel/ConsciousHospitableBooleanvalue

  • Man, thank you so much!

  • 1

    My mistake was that when I previously declared them as double I did not put the corresponding %lf scanf, there was not going :@

  • I really thought it was weird that it didn’t work with all the variables being double, debug or display the values in the console helps a lot in these cases! :D

Browser other questions tagged

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