WAP to create an arithmetic calculator using the switch-case statement

C program to create a menu-driven calculator that performs basic arithmetic operations (add, subtract, multiply and divide) using switch case and functions. The calculator should input two numbers and an operator from the user. It should perform an operation according to the operator entered and must take input in the given format.

create an arithmetic calculator using the switch-case statement
create an arithmetic calculator using the switch-case statement
Here Online Best C++ Emulator Click To Run

<number 1> <operator> <number 2>

  1. Input two numbers and a character from the user in the given format. Store them in some variable say num1op and num2.
  2. Switch the value of op i.e. switch(op).
  3. There are four possible values of op i.e. ‘+’‘-‘‘*’ and ‘/’.
  4. For case '+' perform addition and store result in some variable i.e. result = num1 + num2.
  5. Similarly for case '-' perform subtraction and store result in some variable i.e. result = num1 - num2.
  6. Repeat the process for multiplication and division.
  7. Finally, print the value of the result.

C program to simulate a simple calculator that performs arithmetic operations like addition, subtraction, multiplication, and division only on integers.

/**
 * C program to create simple calculator using switch case and functions
 */

#include <stdio.h>


/** 
 * Function declarations for calculator
 */
float add(float num1, float num2);
float sub(float num1, float num2);
float mult(float num1, float num2);
float div(float num1, float num2);



int main()
{
    char op;
    float num1, num2, result=0.0f;

    /* Print welcome message */
    printf("WELCOME TO SIMPLE CALCULATOR\n");
    printf("----------------------------\n");
    printf("Enter [number 1] [+ - * /] [number 2]\n");

    /* Input two number and operator from user */
    scanf("%f %c %f", &num1, &op, &num2);

    switch(op)
    {
        case '+': 
            result = add(num1, num2);
            break;

        case '-': 
            result = sub(num1, num2);
            break;

        case '*': 
            result = mult(num1, num2);
            break;

        case '/': 
            result = div(num1, num2);
            break;

        default: 
            printf("Invalid operator");
    }

    /* Print the result */
    printf("%.2f %c %.2f = %.2f", num1, op, num2, result);

    return 0;
}


/**
 * Function to add two numbers
 */
float add(float num1, float num2)
{
    return num1 + num2;
}

/**
 * Function to subtract two numbers
 */
float sub(float num1, float num2)
{
    return num1 - num2;
}

/**
 * Function to multiply two numbers
 */
float mult(float num1, float num2)
{
    return num1 * num2;
}

/**
 * Function to divide two numbers
 */
float div(float num1, float num2)
{
    return num1 / num2;
}

Output Arithmetic Calculator using switch case Statements

Get a binary arithmetic expression and solve the expression.

Sample Input 1:

15+6

Sample Output 1:

21

Sample Input 2:

5*5

Sample Output 2:

25

WAP to create an arithmetic calculator using the switch-case statement 1

Note: %.2f is used to print fractional values only up to two decimal places. You can also use %f to print fractional values normally up to six decimal places.

Disclaimer: 

Androbose.in does not own this book/materials, neither created nor scanned. we provide the links which are already available on the internet. For any quarries, Disclaimer are requested to kindly contact us – [email protected] Or https://androbose.in/contact/ This copy was provided for students who are financially troubled but deserving to learn to thank You

Shopping Cart
Scroll to Top