C program to create calculator using switch-case

Ex: Write a C program to create calculator using switch-case. How to write a C program to create calculator using switch-case. C program to create calculator using switch-case.

Input from user:

Enter Number1[+,-,*,/] Number 2:
125/25

Expected output:

Answer is= 5.000000




    Quick links:


  Step by step logic to create calculator using switch-case:


1. Accept two numbers and operator from user variable say no1, no2 , op.

2. Switch the value of op using switch(op).

3. There are four possible values of op +, -, *, /.

4. For case '+' perform addition and  print this, similarly for case '-' perform subtraction and print this using printf.

5. Repeat this process for multiplication and division.

6. In the last for default case print Invalid Operator.



  Program to create calculator using switch-case:


#include<stdio.h>
int main()
{
    char op;
    float no1, no2;

 printf("\t  SIMPLE CALCULATOR\n");
 printf("-------------------------------------\n");
 printf("Enter  Number1 [+,-,*,/] Number2 \n");
scanf("%f%c%f", &no1,&op,&no2);

 switch(op)
    {
        case '+': 
    printf("Answer= %f",no1+no2);
        break;

        case '-': 
    printf("Answer= %f",no1-no2);
        break;

        case '*': 
    printf("Answer= %f",no1*no2);
        break;

        case '/': 
    printf("Answer= %f",no1/no2);
        break;

        default
     printf("Invalid operator");
    }
    
    return 0;
}



Above program shows the following output:


C program to create calculator using switch-case, Create calculator using switch case in C programming language

No comments:

Post a Comment

If you have any doubts, please discuss here...👇