Description
In this program, user will enter operator(+,-,*,/) and two operands. It will give output based on entered operator.
C/C++
/* C Program of Simple Calculator using switch*/ //Save it as SimpleCalculator.c #include<stdio.h> int main(){ double num1, num2; char op; printf("Enter the operation(+,-,x,/) : "); scanf("%c",&op); printf("Enter first number : "); scanf("%lf",&num1); printf("Enter second number : "); scanf("%lf",&num2); switch(op){ case '+': printf("Summation of %.2lf and %.2lf : %.2lf",num1,num2,num1+num2); break; case '-': printf("Substraction of %.2lf and %.2lf : %.2lf",num1,num2,num1-num2); break; case '*': printf("Multiplication of %.2lf and %.2lf : %.2lf",num1,num2,num1*num2); break; case '/': printf("Division of %.2lf and %.2lf : %.2lf",num1,num2,num1/num2); break; default: printf("Invalid operator"); } return 0; }
Input: Enter the operation(+,-,x,/) : + Enter first number : 5 Enter second number : 6 Output: Summation of 5.0 and 6.0 : 11.0
Java
/* Java Program of Simple Calculator using switch*/ //Save it as SimpleCalculator.java import java.io.*; import java.util.Scanner; public class SimpleCalculator { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); double num1, num2; char op; System.out.println("Enter the operation(+,-,x,/) : "); op = scanner.next().charAt(0); System.out.println("Enter first number : "); num1 = scanner.nextDouble(); System.out.println("Enter second number : "); num2 = scanner.nextDouble(); switch(op){ case '+': System.out.println("Summation of "+num1+" and "+num2+" : "+(num1+num2)); break; case '-': System.out.println("Substraction of "+num1+" and "+num2+" : "+(num1-num2)); break; case '*': System.out.println("Multiplication of "+num1+" and "+num2+" : "+(num1*num2)); break; case '/': System.out.println("Division of "+num1+" and "+num2+" : "+(num1/num2)); break; default: System.out.println("Invalid operator"); } } }
Input: Enter the operation(+,-,x,/) : + Enter first number : 5 Enter second number : 6 Output: Summation of 5.0 and 6.0 : 11.0
Related Programs
1) Simple Calculator Using If Else2) Menu Driven Simple Calculator Using If Else
3) Program to calculate factorial using Recursion
4) Program to Check Leap Year
5) Program to find the average of numbers in a given range
6) Program to calculate Gross Salary
7) Program to find modulus of two numbers
8) Program to Display Fibonacci Series
9) Program to Display Fibonacci Series using Recursion
10) Program to find LCM(Least Common Multiple)
11) Program to Swap Two Numbers
12) Program to calculate power of a number
13) Program to Check Whether a Number is Prime or Not
14) Program to find HCF(Highest Common Factor)/GCD(Greatest Common Divisor) and LCM(Least Common Multiple)