Simple calculator program using switch case in c programming



A Calculator is a small electronic device used to perform various arithmetic operations like addition, subtraction, multiplication, division, percentage, etc. It makes our calculations easier and faster. It is a portable device that can use anywhere to perform simple mathematical operations. We use a scientific or sophisticated calculator in some situations, where we need to solve complex calculations like trigonometry functions, exponential operators, degrees, radians, log functions, hyperbolic functions etc. Let's discuss the various ways to create a calculator program in the C language.

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.

Algorithm

1 Step: BEGIN.

2 Step:ENTER TWO OPERANDS FOR OPERATION.

3 Step:ENTER  OPERATOR FOR OPERATION.

4 Step: USER WILL ENTER +,-,*,/,% .

5 Step: SWITCH(OPERATOR)

6 Step: DO THE OPERATION.

7 Step: PRINT THE RESULT.

8 Step: EXIT.


 Flowchart


 #include <stdio.h
 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);
 }

Output

Enter first number: 12                                                                                
Enter second number: 20                                                                               
Choose operation to perform (+,-,*,/,%): +                                                            
Result: 12 + 20 = 32.000000 

                                                                               
Enter first number: 20                                                                               
Enter second number: 5                                                                               
Choose operation to perform (+,-,*,/,%): -                                                           
Result: 20 - 5 = 15.000000                                                                                                                                                                                                                           
Enter first number: 24                                                                               
Enter second number: 12                                                                              
Choose operation to perform (+,-,*,/,%): /                                                           
Result: 24 / 12 = 2.000000                                                           
                                                                                              
Enter first number: 25                                                                               
Enter second number: 5                                                                               
Choose operation to perform (+,-,*,/,%): *                                                           
Result: 25 * 5 = 125.000000                                                               
                                                                               
Enter first number: 11                                                                               
Enter second number: 2                                                                               
Choose operation to perform (+,-,*,/,%): %                                                           
Result: 11 % 2 = 1.000000                                                                    

Post a Comment

0 Comments