C Program to Make a Simple Calculator Using Switch Case



Example to create a basic calculator to add, subtract, multiply, divide and find a percentage.

How this program work?

First, this program will take two operands from user i.e. first-number and the second-number, after that it will ask the user to choose the operator for addition, subtraction, multiply, divide.

Then, the program will print the answer of two operands depending upon the operator chosen by the user.

C Program to Make a Calculator Using Switch Case

#include <stdio.h>

int main()
{
    int num1,num2;
    float result;
    char ope;

    printf("Enter first number: \n");
    scanf("%i",&num1);

    printf("Enter second number: \n");
    scanf("%i",&num2);

    printf("Choose any of these operator : + | - | * | / | % %  ");
    scanf(" %c",&ope);

    //starting of switch
    switch(ope)
    {
        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: %f\n", result);
    return 0;
}

Related:



Comments

Popular posts from this blog

Pseudocode to Check, Number is Odd or Even

How to Create Marksheet in C