C program to create calculator using switch case and functions

Write a C program to create a simple calculator that performs basic arithmetic operations (add, subtract, multiply and divide) using switch case and functions. The calculator should read two numbers and an operator from user. The calculator should perform operation according to the operator entered.


The calculator should take input in following format:
[number 1] [operator] [number 2]
Example:
Input: 5.2 - 3
Output: 2.2

Required knowledge

Basic C programming, Switch case, Operators

Program using switch case

/**
 * C program to create Simple Calculator using switch case
 */

#include <stdio.h>

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

    /*
     * Prints welcome message and 
     * Reads two number and a character from user
     */
    printf("WELCOME TO SIMPLE CALCULATOR\n");
    printf("----------------------------\n");
    printf("Enter [number 1] [+ - * /] [number 2]\n");

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

    switch(op)
    {
        case '+': result = num1 + num2;
            break;
        case '-': result = num1 - num2;
            break;
        case '*': result = num1 * num2;
            break;
        case '/': result = num1 / num2;
            break;
        default: printf("Invalid operator");
            return 0;
    }

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

    return 0;
} 


Program using function

/**
 * 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;

    /*
     * Prints welcome message and 
     * Reads two number and a character from user
     */
    printf("WELCOME TO SIMPLE CALCULATOR\n");
    printf("----------------------------\n");
    printf("Enter [number 1] [+ - * /] [number 2]\n");

    /* Reads two number and an 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");
         return 0;
    }

    /* Prints the result */
    printf("%.2f %c %.2f = %.2f\n", 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;
}

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.


Output
X
_
WELCOME TO SIMPLE CALCULATOR
----------------------------
Enter [number 1] [+ - * /] [number 2]
22 * 6
22.00 * 6.00 = 132.00

Happy coding ;)


Any doubt or suggestion write here. I will try my best to help. Before posting your code you must escape it to view. To format your source code and use format highlighting, post your source code inside
< code >< pre > -- Your source code -- < /pre >< /code > (Remove spaces from pre and code tags).

No comments:

Post a Comment