Thursday 16 November 2017

C Program to Simulate a Simple Calculator


Code:

#include   stdio.h

void main()
{
    char operator;
    float num1, num2, result;

    printf("Simulation of a Simple Calculator\n");
    printf("*********************************\n");
    printf("Enter two numbers \n");
    scanf("%f %f", &num1, &num2);
    fflush(stdin);
    printf("Enter the operator [+,-,*,/] \n");
    scanf("%s", &operator);
    switch(operator)
    {
    case '+': result = num1 + num2;
        break;
    case '-': result = num1 - num2;
        break;
    case '*': result = num1 * num2;
        break;
    case '/': result = num1 / num2;
        break;
    default : printf("Error in operationn");
        break;
    }
    printf("\n %5.2f %c %5.2f = %5.2f\n", num1, operator, num2, result);
}


Output:

Simulation of a Simple Calculator
*********************************
Enter two numbers
2 3
Enter the operator [+,-,*,/]
+

2.00 +  3.00 =  5.00

$ a.out
Simulation of a Simple Calculator
*********************************
Enter two numbers
50 40
Enter the operator [+,-,*,/]
*

50.00 * 40.00 = 2000.00

$ a.out
Simulation of a Simple Calculator
*********************************
Enter two numbers
500 17
Enter the operator [+,-,*,/]
/

500.00 / 17.00 = 29.41

$ a.out
Simulation of a Simple Calculator
*********************************
Enter two numbers
65000 4700
Enter the operator [+,-,*,/]
-

65000.00 - 4700.00 = 60300.00



More C Programs:












100+ Best Home Decoration Ideas For Christmas Day 2019 To Make Home Beautiful

Best gifts for Christmas Day | Greeting cards for Christmas Day | Gift your children a new gift on Christmas day This Christmas d...