Membuat Program Kalkulator menggunakan Javascript



#include <stdio.h>
int main() {
    char operator;
    double first, second;
    printf("Enter an operator (+, -, *,): ");
    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 is not correct");
    }

    return 0;
}

Output

Enter an operator (+, -, *,): *
Enter two operands: 1.5
4.5
1.5 * 4.5 = 6.8

The * operator entered by the user is stored in operator. And, the two operands, 1.5 and 4.5 are stored in first and second respectively.

Since the operator * matches case '*':, the control of the program jumps to

printf("%.1lf * %.1lf = %.1lf", first, second, first * second);

This statement calculates the product and displays it on the screen.

Finally, the break; statement ends the switch statement.

Komentar

Postingan Populer