Calculator in C (Beginner) [duplicate]

-3

I have a question in the following exercise:

  

Create a program that receives two integers and shows the   result of the division of numbers and their remainder. the program should   whether the user wishes to repeat the operation or close the   program.

I do not know how to do it so that at the end of the program if I type 1, it ends, if you type 2 start again, here is my program:

int main() 
{   
    int var1, var2, Q, R;
    printf("Digite o dividendo: ");
    scanf("%d", &var1);
    printf("Digite o divisor: ");
    scanf("%d", &var2);
    Q = var1 / var2;
    R = var1 % var2;
    printf("Resultado: %d\n", Q);
    printf("Resto: %d\n", R);

    int decisao;
    printf("\nDeseja encerrar o programa? \n1 para sim e 2 para nao.");
    scanf("%d", &decisao);
}
    
asked by anonymous 04.06.2018 / 15:58

1 answer

2

You can use a loop repetition or at the end of your code, you can add if to check if the value contained in the decision variable equals two, if the comparison result is true then you can call the method main() again.

Example with loop Do While :

int main() 
{   
    int var1, var2, Q, R, decisao = 2;

    do {
        printf("Digite o dividendo: ");
        scanf("%d", &var1);
        printf("Digite o divisor: ");
        scanf("%d", &var2);
        Q = var1 / var2;
        R = var1 % var2;
        printf("Resultado: %d\n", Q);
        printf("Resto: %d\n", R);

        printf("\n Caso deseje repetir a operacao digite 2 ou digite qualquer outro valor para encerrar.\n");
        scanf("%d", &decisao);

    } while(decisao == 2);

}

Example with recursion:

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

int main() 
{   
    int var1, var2, Q, R;
    printf("Digite o dividendo: ");
    scanf("%d", &var1);
    printf("Digite o divisor: ");
    scanf("%d", &var2);
    Q = var1 / var2;
    R = var1 % var2;
    printf("Resultado: %d\n", Q);
    printf("Resto: %d\n", R);

    int decisao;
    printf("\n Caso deseje repetir a operacao digite 2 ou digite qualquer outro valor para encerrar.\n");
    scanf("%d", &decisao);

    if(decisao == 2)
        return main();  

}

Note, I've changed the message because the program will exit for any value other than two.

    
04.06.2018 / 16:39