Monday 18 March 2019

Calculator in C programming..

C program for a Simple calculator..

# include <stdio.h>

int main() {

    char operator;
    double first,second;

    printf("Enter an operator for your desired operation (+, -, *,/ ");
    scanf("%c", &operator);

    printf("Enter two operands: ");
    scanf("%lf %lf",&first, &second);

    switch(operator)
    {
        case '+':
            printf("%.1lf + %.1lf = %.1lf",first, second, first + second);
            break;

        case '-':
            printf("%.1lf - %.1lf = %.1lf",first, second, first - second);
            break;

        case '*':
            printf("%.1lf * %.1lf = %.1lf",first, second, first * second);
            break;

        case '/':
            printf("%.1lf / %.1lf = %.1lf",first, second, first / second);
            break;

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


0 comments:

Post a Comment

 
biz.