-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.
Man, thank you so much!
– Jeferson Tomaz
My mistake was that when I previously declared them as double I did not put the corresponding %lf scanf, there was not going :@
– Jeferson Tomaz
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– Daniel Mendes