Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

please write program in C language and show that it works in the compiler. 10, w

ID: 3828881 • Letter: P

Question


please write program in C language and show that it works in the compiler.

10, write a program to model a simple calculator. Each data line should consist of the next operation to be performed from the list below and the right operand. Assume the left operand is the accumulator value (initial value of 0). You need a function scan data with two output parameters that returns the operator and right operand scanned from a data line. You need a function do next op that performs the required operation. do next op has two input parameters the operator and operand) and one input/output parameter (the accumulator The valid operators are: add subtract multiply divide power (ralse left operand to power of right operand) q quit Your calculator should display the accumulator value after each operation. A sample nun follows. result so far is 5.0 result so far is 25.0 result ao far 12.5 final result 18 12.5

Explanation / Answer

#include <stdio.h>

int main()
{
    char yes;
    int a, b, c, choice;
    printf("Enter first integer: ");
    scanf("%d", &a);

    yes = 'y';
    while(yes == 'y' || yes == 'Y')
    {
        printf("Enter new integer: ");
        scanf("%d", &b);

        printf(" Add(1), Subtract(2), Multiply(3), Divide(4): ");
        scanf("%d", &choice);

        printf(" ");
        switch(choice)
        {
                case(1):
                    c = a + b;
                    printf("%d + %d = %d ", a, b, c);
                    break;
                case(2):
                    c = a - b;
                    printf("%d - %d = %d ", a, b, c);
                    break;
                case(3):
                    c = a * b;
                    printf("%d * %d = %d ", a, b, c);
                    break;
                case(4):
                    c = a / (float)b;
                    printf("%d / %d = %d ", a, b, c);
                    break;
                default:
                    printf("Incorrect choice. Try again. ");
        }

        printf(" Again (Y/N): ");
        scanf(" %c", &yes);
        a=c;
    }

    return 0;
}