Hello There, Guest! Login Register
Advertise Here | Remove Ads

C Program to Make a Simple Calculator

  • 0 Vote(s) - 0 Average


08-11-2016, 09:49 PM #1
Sadness Offline MODERATOR MODERATORS
Posts:4,001 Threads:565 Joined:Sep 2014 Reputation: 13 Sex Female
Country: Bangladesh
This program takes an arithmetic operator +, -, *, / and two operands from the user and performs the calculation on the two operands depending upon the operator entered by the user.


Code:
# include <stdio.h>

int main() {

    char operator;
    double firstNumber,secondNumber;

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

    printf("Enter two operands: ");
    scanf("%lf %lf",&firstNumber, &secondNumber);

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

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

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

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

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

[Image: SeoClerks728x90.png]






Forum Jump:


Users browsing this thread:1 Guest(s)

Advertise Here | Remove Ads