Calculator Program in C (3 Examples)

Calculator Program in C (3 Examples)

  • Blog
  • 2 mins read

Below are the 3 examples of calculator programs in the C language.

Example 1:

In the following C program example, it will first ask the user to input an operand (+, -  or *) and then will ask for the two numbers and then will give the calculated value:

#include 
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;
    default:
        printf("Error! operator is not correct");
    }

    return 0;
}

Output:

Enter an operator (+, -, *,): +
Enter two operands: 2 4
2.0 + 4.0 = 6.0

Example 2:

In the following C program, first, it will ask the user to enter the first number and then the second number, and then the operator:

#include 
//Simple Calculator
int main() {

        int fno;
        int sno;
        int rst;
       char opr;
       printf ( "Enter First Number: ");
       scanf("%d", &fno);
       printf ("Enter Second Number: ");
       scanf("%d", &sno);
       printf ("Type Operator : ");
       scanf(" %c", &opr);
       rst = fno + sno;
    if (opr == '+')
    
        printf ("%d \n", fno+sno);
    
      else (opr == '-');
        printf ("%d \n", fno-sno);

    if (opr == '*')
    
        printf("%d \n", fno*sno);

      else (opr == '/');
        printf ("%d \n", fno/sno);
}

Output:

Enter First Number: 5
Enter Second Number: 3
Type Operator : *
2 
15 
1

Example 3:

The following C program uses the same input logic as above but having a different program logic:

#include 
 
int main()
{
    int num1,num2;
    float result;
    char ch;    //to store operator choice
     
    printf("Enter first number: ");
    scanf("%d",&num1);
    printf("Enter second number: ");
    scanf("%d",&num2);
     
    printf("Choose operation to perform (+,-,*,/,%): ");
    scanf(" %c",&ch);
     
    result=0;
    switch(ch)    
    {
        case '+':
            result=num1+num2;
            break;
             
        case '-':
            result=num1-num2;
            break;
         
        case '*':
            result=num1*num2;
            break;
             
        case '/':
            result=(float)num1/(float)num2;
            break;
             
        case '%':
            result=num1%num2;
            break;
        default:
            printf("Invalid operation.\n");
    }
 
    printf("Result: %d %c %d = %f\n",num1,ch,num2,result);
    return 0;
}

Output:

Enter first number: 4
Enter second number: 2
Choose operation to perform (+,-,*,/,%): -
Result: 4 - 2 = 2.000000