Header Ads

32 - WAP of a Calculator with Add, Sub, Mult, & Div in C

Calculator program with the operation's of Addition, Subtraction, Multiplication & Division in C.

Program Code :

// Performs addition, subtraction, multiplication or division depending the input from user
# include <stdio.h>
int main() 
{
    char operator;
    double firstNumber,secondNumber;

    printf("Enter an operator (+, -, *, /): ");
    scanf("%c", &operator);

    printf("Enter 1st Operand : ");
    scanf("%lf",&firstNumber);

    printf("Enter 2nd Operand : ");
    scanf("%lf",&secondNumber);
 
    switch(operator)
    {
        case '+':
            printf("Addition: %.2lf + %.2lf = %.2lf\n",firstNumber, secondNumber, firstNumber + secondNumber);
            break;

        case '-':
            printf("Subtraction: %.2lf - %.2lf = %.2lf\n",firstNumber, secondNumber, firstNumber - secondNumber);
            break;

        case '*':
            printf("Multiplication: %.2lf * %.2lf = %.2lf\n",firstNumber, secondNumber, firstNumber * secondNumber);
            break;

        case '/':
            printf("Division: %.2lf / %.2lf = %.2lf\n",firstNumber, secondNumber, firstNumber / secondNumber);
            break;

        // operator doesn't match any case constant (+, -, *, /)
        default:
            printf("Error! Invalid Operator :(");
    }   
    return 0;
} 

Program Output: 

 

No comments