In this example, you will learn to create a  
 c program To make a simple calculator.






  • To Understand The program you need the basic Knowledge of

      1)  C switch statement
     2)  Break and continue


Switch case Syntax


switch (expression)
​{
    case constant1:
      // statements
      break;

    case constant2:
      // statements
      break;
    .
    .
    .
    default:
      // default statements
}

  • This program takes an arithmetic operator from the user Like this (+,-,*,/) .And two operands from the user (or you can say that two numbers input from the user).

  • Then it performs the calculation on the two operands according to the operator that user entered. 




  • Here, we will create a simple calculator program in C which will perform basic arithmetic operations like addition, subtraction, multiplication, division, and remainder depending on the input from the user.

  • Users will have the choice to choose the operation. If the user chooses the wrong operation then he/she will get an error message.


Simple Calculator Using Switch statement


#include <stdio.h>
int main()

{
char op;
double first, second;
printf("Enter an operator (+, -, *, /): ");
scanf("%c", &op);
printf("Enter two operands: ");
scanf("%lf %lf", &first, &second); switch (op) {
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




Explanation 


  • The * operator entered by the user is stored in op. 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.
  • To make our output look cleaner, we have simply limited the output to one decimal place using the code %.1lf.
  • Finally, the break; statement ends the switch statement.

  •  If the entered operator is not matched with the case labels then the default statement will be executed and it will display the message
  •  "Error! operator is not correct"


Enter an operator (+, -, *,): %
Enter two operands: 1.5
4.5
 "Error! operator is not correct"


EXAMPLE-2  (Using else -if statement)

In The previous example we use the switch case to perform a c program to make a simple calculator.

And in this example we make a c program to make a simple calculator using simple if else-if statement.


  • To Understand The program you need the basic Knowledge of

      1)  C else if _ if else
      2)  c operator


/* C Program to Create Simple Calculator using Else If */

 

#include <stdio.h>

 

int main()

{

    char Operator;

    float num1, num2, result = 0;

    

    printf("\n Please Enter an Operator (+, -, *, /)  :  ");

    scanf("%c", &Operator);

    

    printf("\n Please Enter the Values for two Operands: num1 and num2  :  ");

    scanf("%f%f", &num1, &num2);

    

    if(Operator == '+')

    {

        printf("\n The result of %.2f + %.2f  = %.2f", num1, num2, num1 + num2);

    }

    else if(Operator == '-')

    {

        printf("\n The result of %.2f - %.2f  = %.2f", num1, num2, num1 - num2);

    }

    else if(Operator == '*')

    {

        printf("\n The result of %.2f * %.2f  = %.2f", num1, num2, num1 * num2);

    }

    else if(Operator == '/')

    {

        printf("\n The result of %.2f / %.2f  = %.2f", num1, num2, num1 / num2);

    }

    else

    {

        printf("\n You have enetered an Invalid Operator ");

    }

    

    return 0;

}


Output

Please Enter an Operator (+, -, *, /)  :  +

Please Enter the Values for two Operands: num1 and num2  :  14 2

The result of 14 + 2  = 16


Explanation

  • The (+) operator entered by the user is stored in (operator). And, the two operands, 14  and 2 are stored in first and second respectively.

  • Then it check all The else-if condition Since the operator  (+) matches  
  •  the control of the program jumps to. This




if(Operator == '+')
    {
        printf("\n The result of %.2f + %.2f  = %.2f", num1, num2, num1 + num2);
    }


Also check Out





  Q-2)  pointers in c




  

   Q-3) Vector Sort


Also check Out Projects for Resume

    click here

Projects on C++


Projects on C


Problems On C


Problems On C++




Feedback!!

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or you find anything incorrect? Let us know in the comments. Thank you!

Once again Thank you for Reading this !!!


Happy Coding!!

Ramji!!!