Q. Write a program to make a simple calculator.
Calculator :- A calculator is used to make mathematical calculations. In this program we will create a simple calculator that can perform an arithmetic operation (+, -, *, /).
For Example :-
Enter an operator (+, -, *, /): *
Enter two operands: 1.5 4.5
1.5 * 4.5 = 6.8
So as we can see that first we have to choose which arithmetic operation we want to do, then after selecting two operands for which we have to do arithmetic calculation.
Calculator Program Algorithm
START Step 1 : Initialise the two numbers. Step 2 : Ask the user to enter an option by giving six options. Step 3 : After getting the option from the user write if conditions for every operation based on the option. Step 4 : Perform the respective operation. Step 5 : Print the result. STOP
Calculator Program
#include <stdio.h> int main() { char ch; double num1, num2; printf("Enter an operator (+, -, *, /): "); scanf("%c", &ch); printf("Enter two operands: "); scanf("%lf %lf", &num1, &num2); switch (ch) { case '+': printf("%.1lf + %.1lf = %.1lf", num1, num2, num1 + num2); break; case '-': printf("%.1lf - %.1lf = %.1lf", num1, num2, num1 - num2); break; case '*': printf("%.1lf * %.1lf = %.1lf", num1, num2, num1 * num2); break; case '/': printf("%.1lf / %.1lf = %.1lf", num1, num2, num1 / num2); break; // operator doesn't match any case constant default: printf("Error! operator is not correct"); } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { char ch; float num1, num2; cout << "Enter an operator (+, -, *, /): "; cin >> ch; cout << "Enter two operands: "; cin >> num1 >> num2; switch(ch) { case '+': cout << num1+num2; break; case '-': cout << num1-num2; break; case '*': cout << num1*num2; break; case '/': cout << num1/num2; break; default: // If the operator is other than +, -, * or /, error message is shown cout << "Error! operator is not correct"; break; } return 0; }
import java.util.Scanner; public class LFC { public static void main(String[] args) { Scanner reader = new Scanner(System.in); System.out.print("Enter two operands: "); // nextDouble() reads the next double from the keyboard double num1 = reader.nextDouble(); double num2 = reader.nextDouble(); System.out.print("Enter an operator (+, -, *, /): "); char ch = reader.next().charAt(0); double res; switch(ch) { case '+': res = num1 + num2; break; case '-': res = num1 - num2; break; case '*': res = num1 * num2; break; case '/': res = num1 / num2; break; // operator doesn't match any case constant (+, -, *, /) default: System.out.printf("Error! operator is not correct"); return; } System.out.printf("%.1f %c %.1f = %.1f", num1, ch, num2, res); } }
# Program make a simple calculator # This function adds two numbers def add(x, y): return x + y # This function subtracts two numbers def subtract(x, y): return x - y # This function multiplies two numbers def multiply(x, y): return x * y # This function divides two numbers def divide(x, y): return x / y # Take input from the user ch = input("Enter an operator (+, -, *, /): ") num1 = float(input("Enter first number: ")) num2 = float(input("Enter second number: ")) if ch == '+': print(num1,"+",num2,"=", add(num1,num2)) elif ch == '-': print(num1,"-",num2,"=", subtract(num1,num2)) elif ch == '*': print(num1,"*",num2,"=", multiply(num1,num2)) elif ch == '/': print(num1,"/",num2,"=", divide(num1,num2)) else: print("Error! operator is not correct")
#include <stdio.h> int main() { char ch; double num1, num2; printf("Enter an operator (+, -, *, /): "); scanf("%c", &ch); printf("Enter two operands: "); scanf("%lf %lf", &num1, &num2); switch (ch) { case '+': printf("%.1lf + %.1lf = %.1lf", num1, num2, num1 + num2); break; case '-': printf("%.1lf - %.1lf = %.1lf", num1, num2, num1 - num2); break; case '*': printf("%.1lf * %.1lf = %.1lf", num1, num2, num1 * num2); break; case '/': printf("%.1lf / %.1lf = %.1lf", num1, num2, num1 / num2); break; // operator doesn't match any case constant default: printf("Error! operator is not correct"); } return 0; }
Output
Enter an operator (+, -, *, /): * Enter two operands: 8 6 8.0 * 6.0 = 48.0
Recommended Programs
Program to find LCM of 2 Numbers.Program to print fibonacci series..