Create Simple Calculator in C / C++ / Java / Python / C#

Simple calculator

Programs to create a simple calulator have been shown here. In this calculator, the user can do addition, subtraction, multiplication, division and power operations.






1. Programs to create simple calculator

The following section covers the C, C++, Java, Python and C# programs to make a simple calculator. The user provides an arithmetic expression as the input, and it is evaluated to produce the output. The input expression consists of two operands and one operator (addition, multiplication, subtraction, division and power). The syntax of the input expression is <First operand> <Operator> <Second operand>.




1.1. C Program to create simple calculator

Code has been copied
/*******************************
      alphabetacoder.com
C program for simple calculator
********************************/

#include <stdio.h>
#include <math.h>

int main() {
    // declare variables
    double f, l;
    char op;

    // take input 
    // input consist of three terms
    // two operands and one operator
    printf("Allowed opeators: (+, -, *, /, ^)\n");
    printf("Enter the expression  (operand1 operator operand2): ");
    scanf("%lf %c %lf", & f, & op, & l);

    // calculate value according to 
    // the operator in input expression
    switch (op) {
        // for addition
    case '+':
        printf("%lf + %1lf = %lf", f, l, f + l);
        break;
        // for subtraction
    case '-':
        printf("%lf - %1lf = %lf", f, l, f - l);
        break;
        // for multiplication
    case '*':
        printf("%lf * %1lf = %lf", f, l, f * l);
        break;
        // for division
    case '/':
        printf("%lf / %1lf = %lf", f, l, f / l);
        break;
        // for power
    case '^':
        printf("%lf ^ %1lf = %lf", f, l, pow(f, l));
        break;
        // for wrong operator
    default:
        printf("Input operator is not correct!");
    }

    return 0;
}

Output


Case 1:

Allowed opeators: (+, -, *, /, ^)

Enter the expression (operand1 operator operand2): 3.5 + 4.65

3.500000 + 4.650000 = 8.150000


Case 2:

Allowed opeators: (+, -, *, /, ^)

Enter the expression (operand1 operator operand2): 8.5 * 5

8.500000 * 5.000000 = 42.500000


Case 3:

Allowed opeators: (+, -, *, /, ^)

Enter the expression (operand1 operator operand2): 10 * 3.2

10.000000 ^ 3.200000 = 1584.893192




1.2. C++ Program to create simple calculator

Code has been copied
/********************************
      alphabetacoder.com
C++ program for simple calculator
*********************************/

#include <iostream>
#include <cmath>

using namespace std;

int main() {
    // declare variables
    double f, l;
    char op;

    // take input 
    // input consist of three terms
    // two operands and one operator
    cout << "Allowed opeators: (+, -, *, /, ^)\n";
    cout << "Enter the expression  (operand1 operator operand2): ";
    cin >> f;
    cin >> op;
    cin >> l;

    // calculate value according to 
    // the operator in input expression
    switch (op) {
        // for addition
	case '+':
            cout << f <<" + " << l <<" = " << f + l;
	    break;
	// for subtraction
	case '-':
	    cout << f <<" - " << l <<" = " << f - l;
	    break;
	// for multiplication
	case '*':
	    cout << f <<" * " << l <<" = " << f * l;
	    break;
	// for division
	case '/':
	    cout << f <<" / " << l <<" = " << f / l;
	    break;
	// for power
	case '^':
	    cout << f <<" ^ " << l <<" = " << pow(f, l);
	    break;
	// for wrong operator
	default:
	    printf("Input operator is not correct!");
    }

    return 0;
}

Output


Case 1:

Allowed opeators: (+, -, *, /, ^)

Enter the expression (operand1 operator operand2): 3.5 + 6.2

3.5 + 6.2 = 9.7


Case 2:

Allowed opeators: (+, -, *, /, ^)

Enter the expression (operand1 operator operand2): 345 * 21.5

345.000000 * 21.500000 = 7417.500000


Case 3:

Allowed opeators: (+, -, *, /, ^)

Enter the expression (operand1 operator operand2): 100 / 33.33

100.000000 / 33.330000 = 3.000300




1.3. Java Program to create simple calculator

Code has been copied
/*********************************
      alphabetacoder.com
Java program for simple calculator
**********************************/

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        // declare variables
        double f, l;
        char op;

        // declare instance of Scanner class
        Scanner sc = new Scanner(System.in);

        // take input
        System.out.println("Allowed opeators: (+, -, *, /, ^)");
        System.out.print("Enter the expression  (operand1 operator operand2): ");
        f = sc.nextDouble();
        op = sc.next().charAt(0);
        l = sc.nextDouble();

        // calculate value according to 
        // the operator in input expression
        // round off to 4 decimal places
        switch (op) {
            // for addition
        case '+':
            System.out.println(f + " + " + l + " = " + (double) Math.round((f + l) * 10000) / 10000);
            break;
            // for subtraction
        case '-':
            System.out.println(f + " - " + l + " = " + (double) Math.round((f - l) * 10000) / 10000);
            break;
            // for multiplication
        case '*':
            System.out.println(f + " * " + l + " = " + (double) Math.round((f * l) * 10000) / 10000);
            break;
            // for division
        case '/':
            System.out.println(f + " / " + l + " = " + (double) Math.round((f / l) * 10000) / 10000);
            break;
            // for power
        case '^':
            System.out.println(f + " ^ " + l + " = " + (double) Math.round((Math.pow(f, l)) * 10000) / 10000);
            break;
            // for wrong operator
        default:
            System.out.println("Input operator is not correct!");
        }
    }
}

Output


Case 1:

Allowed opeators: (+, -, *, /, ^)

Enter the expression (operand1 operator operand2): 4.5 ^ 2

4.5 ^ 2.0 = 20.25


Case 2:

Allowed opeators: (+, -, *, /, ^)

Enter the expression (operand1 operator operand2): 4.71 + 8.34

4.71 + 8.34 = 13.05


Case 3:

Allowed opeators: (+, -, *, /, ^)

Enter the expression (operand1 operator operand2): 49 / 41

49.0 / 41.0 = 1.1951




1.4. Python Program to create simple calculator

Code has been copied
#************************************
#       alphabetacoder.com
#Python program for simple calculator
#************************************/

# take input 
# input consist of three terms
# two operands and one operator
print("Allowed opeators: (+, -, *, /, ^)")
[f, op, l] = input("Enter the expression  (operand1 operator operand2): ").split()

# convert to float
f = float(f)
l = float(l)

# calculate value according to 
# the operator in the input expression
# round to 4 decimal places
# for addition
if op == '+':
    print(f, "+",l ,"=", round(f + l, 4))
# for subtraction
elif op == '-':
    print(f, "-",l ,"=", round(f - l, 4))
# for multiplication
elif op == '*':
    print(f, "*",l ,"=", round(f * l, 4))
# for division
elif op == '/':
    print(f, "/",l ,"=", round(f / l, 4))
# for power
elif op == '^':
    print(f, "^",l ,"=", round(f ** l, 4))
# for wrong operator
else:
    print("Input operator is not correct!")

Output


Case 1:

Allowed opeators: (+, -, *, /, ^)

Enter the expression (operand1 operator operand2): 7.9 ^ 4.8

7.9 ^ 4.8 = 20352.138


Case 2:

Allowed opeators: (+, -, *, /, ^)

Enter the expression (operand1 operator operand2): 45 - 25.75

45.0 - 25.75 = 19.25


Case 3:

Allowed opeators: (+, -, *, /, ^)

Enter the expression (operand1 operator operand2): 7 / 9

7.0 / 9.0 = 0.7778




1.5. C# Program to create simple calculator

Code has been copied
/*******************************
      alphabetacoder.com
C# program for simple calculator
********************************/

using System;

namespace Calculator {
    class Program {
        static void Main(string[] args) {
            // declare variables
            double f, l;
            char op;
            string[] input;

            // take input in single line
            // input consist of three terms
            // two operands and one operator
            Console.WriteLine("Allowed opeators: (+, -, *, /, ^)");
            Console.Write("Enter the expression  (operand1 operator operand2): ");
            input = Console.ReadLine().Split();

            // fetch the value of two operands and one operator
            f = double.Parse(input[0]);
            op = char.Parse(input[1]);
            l = double.Parse(input[2]);

            // calculate value according to 
            // the operator in input expression
            // Round off to 4 decimal places
            switch (op) {
                // for addition
            case '+':
                Console.WriteLine(f + " + " + l + " = " + Math.Round((f + l), 4));
                break;
                // for subtraction
            case '-':
                Console.WriteLine(f + " - " + l + " = " + Math.Round((f - l), 4));
                break;
                // for multiplication
            case '*':
                Console.WriteLine(f + " * " + l + " = " + Math.Round((f * l), 4));
                break;
                // for division
            case '/':
                Console.WriteLine(f + " / " + l + " = " + Math.Round((f / l), 4));
                break;
                // for power
            case '^':
                Console.WriteLine(f + " ^ " + l + " = " + Math.Round(Math.Pow(f, l), 4));
                break;
                // for wrong operator
            default:
                Console.WriteLine("Input operator is not correct!");
                break;
            }

            // wait for user to press any key
            Console.ReadKey();
        }
    }
}

Output


Case 1:

Allowed opeators: (+, -, *, /, ^)

Enter the expression (operand1 operator operand2): 654 / 56

654 / 56 = 11.6786


Case 2:

Allowed opeators: (+, -, *, /, ^)

Enter the expression (operand1 operator operand2): 34 ^ 2.25

34 ^ 2.25 = 2791.4353


Case 3:

Allowed opeators: (+, -, *, /, ^)

Enter the expression (operand1 operator operand2): 33 + 45.345

33 + 45.345 = 78.345