This page is about creating a simple calculator. A good way to do this is to first ask for what kind of operation to do, then ask the first variable, and then the second.
Examples
java
#include <iostream> #include <cstdlib> using namespace std; int main() { double num1, num2; int opselect; cout << "\n welcome."; cout << "this is a simple calculator."; cout << "\n enter the first number: "; cin >> num1; cout << "enter operation: "; cout << "\n 1 = addition"; cout << "\n 2 = subtraction"; cout << "\n 3 = multiplication"; cout << "\n 4 = division"; cout << "\n - "; cin >> opselect; cout << "enter the second number: "; cin >> num2; cout << "CALCULATING"; cout << "."; cout << "."; cout << "."; if (num1 == 2, num2 == 2, opselect == 1) { cout << num1 << " + " << num2 << " = " << "chair" << endl; } else switch (opselect) { case 1: cout << num1 << " + " << num2 << " = " << num1+num2 << endl; break; case 2: cout << num1 << " - " << num2 << " = " << num1-num2 << endl; break; case 3: cout << num1 << " X " << num2 << " = " << num1*num2 << end; break; case 4: cout << num1 << " / " << num2 << " = " << num1/num2 << endl; break; } system("PAUSE"); return 0; }
Java
An open source calculator:
import java.util.Scanner; public class Main { public static void main(String[] args) { System.out.println("DOS-CALC::Created by AntoArts Entertainment"); System.out.println("----------------v1.0.00--------------------"); Scanner scan = new Scanner(System.in); System.out.println("Type the name of the operation you want to use: "); System.out.println(" (addition/subtraction/multiplication/division)"); String operat = scan.next(); if(operat.equals("addition")){ System.out.println("Counting with addition, please type in variable A: "); double a = scan.nextDouble(); System.out.println("Variable A is "+a+", please type in variable B: "); double b = scan.nextDouble(); double z=a+b; System.out.println(a+"+"+b+"="+z); } else if(operat.equals("subtraction")){ System.out.println("Counting with subtraction, please type in variable A: "); double a = scan.nextDouble(); System.out.println("Variable A is "+a+", please type in variable B: "); double b = scan.nextDouble(); double z=a-b; System.out.println(a+"-"+b+"="+z); } else if(operat.equals("multiplication")){ System.out.println("Counting with multiplication, please type in variable A: "); double a = scan.nextDouble(); System.out.println("Variable A is "+a+", please type in variable B: "); double b = scan.nextDouble(); double z=a*b; System.out.println(a+"*"+b+"="+z); } else if(operat.equals("division")){ System.out.println("Counting with division, please type in variable A: "); double a = scan.nextDouble(); System.out.println("Variable A is "+a+", please type in variable B: "); double b = scan.nextDouble(); double z=a/b; System.out.println(a+"/"+b+"="+z); } else{ System.out.println("The operation you've requested wasn't avaiable"); System.out.println("Check spelling and make sure you typed the operation name lowercase"); } } }
import java.util.Scanner; public class Main { public static void main(String[] args) { System.out.println("DOS-CALC::Created by AntoArts Entertainment"); System.out.println("----------------v1.0.00--------------------"); Scanner scan = new Scanner(System.in); System.out.println("Type the name of the operation you want to use: "); System.out.println(" (addition/subtraction/multiplication/division)"); String operat = scan.next(); if(operat.equals("addition")){ System.out.println("Counting with addition, please type in variable A: "); double a = scan.nextDouble(); System.out.println("Variable A is "+a+", please type in variable B: "); double b = scan.nextDouble(); double z=a+b; System.out.println(a+"+"+b+"="+z); } else if(operat.equals("subtraction")){ System.out.println("Counting with subtraction, please type in variable A: "); double a = scan.nextDouble(); System.out.println("Variable A is "+a+", please type in variable B: "); double b = scan.nextDouble(); double z=a-b; System.out.println(a+"-"+b+"="+z); } else if(operat.equals("multiplication")){ System.out.println("Counting with multiplication, please type in variable A: "); double a = scan.nextDouble(); System.out.println("Variable A is "+a+", please type in variable B: "); double b = scan.nextDouble(); double z=a*b; System.out.println(a+"*"+b+"="+z); } else if(operat.equals("division")){ System.out.println("Counting with division, please type in variable A: "); double a = scan.nextDouble(); System.out.println("Variable A is "+a+", please type in variable B: "); double b = scan.nextDouble(); double z=a/b; System.out.println(a+"/"+b+"="+z); } else{ System.out.println("The operation you've requested wasn't avaiable"); System.out.println("Check spelling and make sure you typed the operation name lowercase"); } } }