calculator program in java

Solutions on MaxInterview for calculator program in java by the best coders in the world

showing results for - "calculator program in java"
Henri
07 Jan 2017
1import java.util.Scanner;
2public class Calculator {
3   public static void main(String[] args) {
4      double num1;
5      double num2;
6      double ans;
7      char op;
8      Scanner reader = new Scanner(System.in);
9      System.out.print("Enter two numbers: ");
10      num1 = reader.nextDouble();
11      num2 = reader.nextDouble();
12      System.out.print("\nEnter an operator (+, -, *, /): ");
13      op = reader.next().charAt(0);
14      switch(op) {
15         case '+': ans = num1 + num2;
16            break;
17         case '-': ans = num1 - num2;
18            break;
19         case '*': ans = num1 * num2;
20            break;
21         case '/': ans = num1 / num2;
22            break;
23      default: System.out.printf("Error! Enter correct operator");
24         return;
25      }
26      System.out.print("\nThe result is given as follows:\n");
27      System.out.printf(num1 + " " + op + " " + num2 + " = " + ans);
28   }
29}
Juana
07 Apr 2019
1// this was all coded by me, to use it get Eclipse, or IntelliJ and make a new
2// Java Project
3
4package javaCode;
5
6import java.util.Scanner;
7
8public class testCode {
9
10	public static void main(String[] args) {
11		
12		Scanner scan = new Scanner(System.in);
13		
14		System.out.println("Would you like to Add, Subtract, Divide, or Multiply?");
15		
16		String mathType = scan.next();
17		
18		if (mathType.equalsIgnoreCase("add")) {
19			System.out.println("Whats the first number you want to add?");
20			
21			int firstNumber = scan.nextInt();
22			
23			System.out.println(firstNumber + " + ");
24			System.out.println("Enter your second number");
25			
26			int secondNumber = scan.nextInt();
27			
28			System.out.println(firstNumber + " + " + secondNumber + " =");
29			System.out.println(firstNumber + secondNumber);
30			
31		} else if (mathType.equalsIgnoreCase("subtract")) {
32			System.out.println("Whats the first number you want to subtract?");
33			
34			int firstNumber = scan.nextInt();
35			
36			System.out.println(firstNumber + " - ");
37			System.out.println("Enter your second number");
38			
39			int secondNumber = scan.nextInt();
40			
41			System.out.println(firstNumber + " - " + secondNumber + " =");
42			System.out.println(firstNumber - secondNumber);
43		} else if (mathType.equalsIgnoreCase("divide")) {
44			System.out.println("Whats the first number you want to divide?");
45			
46			int firstNumber = scan.nextInt();
47			
48			System.out.println(firstNumber + " % ");
49			System.out.println("Enter your second number");
50			
51			int secondNumber = scan.nextInt();
52			
53			System.out.println(firstNumber + " % " + secondNumber + " =");
54			System.out.println(firstNumber / secondNumber);
55		} else if (mathType.equalsIgnoreCase("multiply")){
56			System.out.println("Whats the first number you want to multiply?");
57			
58			int firstNumber = scan.nextInt();
59			
60			System.out.println(firstNumber + " x ");
61			System.out.println("Enter your second number");
62			
63			int secondNumber = scan.nextInt();
64			
65			System.out.println(firstNumber + " x " + secondNumber + " =");
66			System.out.println(firstNumber * secondNumber);
67		}
68		
69	}
70
71}
Germaine
01 Jul 2017
1Save this file as MyCalculator.java 
2to compile it use 
3    javac MyCalculator.java 
4to use the calcuator do this 
5    java MyCalculator 
6 
7**********************************************/  
8import java.awt.*;  
9import java.awt.event.*;  
10/*********************************************/  
11  
12public class MyCalculator extends Frame  
13{  
14  
15public boolean setClear=true;  
16double number, memValue;  
17char op;  
18  
19String digitButtonText[] = {"7""8""9""4""5""6""1""2""3""0""+/-""." };  
20String operatorButtonText[] = {"/""sqrt""*""%""-""1/X""+""=" };  
21String memoryButtonText[] = {"MC""MR""MS""M+" };  
22String specialButtonText[] = {"Backspc""C""CE" };  
23  
24MyDigitButton digitButton[]=new MyDigitButton[digitButtonText.length];  
25MyOperatorButton operatorButton[]=new MyOperatorButton[operatorButtonText.length];  
26MyMemoryButton memoryButton[]=new MyMemoryButton[memoryButtonText.length];  
27MySpecialButton specialButton[]=new MySpecialButton[specialButtonText.length];  
28  
29Label displayLabel=new Label("0",Label.RIGHT);  
30Label memLabel=new Label(" ",Label.RIGHT);  
31  
32final int FRAME_WIDTH=325,FRAME_HEIGHT=325;  
33final int HEIGHT=30, WIDTH=30, H_SPACE=10,V_SPACE=10;  
34final int TOPX=30, TOPY=50;  
35///////////////////////////  
36MyCalculator(String frameText)//constructor  
37{  
38super(frameText);  
39  
40int tempX=TOPX, y=TOPY;  
41displayLabel.setBounds(tempX,y,240,HEIGHT);  
42displayLabel.setBackground(Color.BLUE);  
43displayLabel.setForeground(Color.WHITE);  
44add(displayLabel);  
45  
46memLabel.setBounds(TOPX,  TOPY+HEIGHT+ V_SPACE,WIDTH, HEIGHT);  
47add(memLabel);  
48  
49// set Co-ordinates for Memory Buttons  
50tempX=TOPX;   
51y=TOPY+2*(HEIGHT+V_SPACE);  
52for(int i=0; i<memoryButton.length; i++)  
53{  
54memoryButton[i]=new MyMemoryButton(tempX,y,WIDTH,HEIGHT,memoryButtonText[i], this);  
55memoryButton[i].setForeground(Color.RED);  
56y+=HEIGHT+V_SPACE;  
57}  
58  
59//set Co-ordinates for Special Buttons  
60tempX=TOPX+1*(WIDTH+H_SPACE); y=TOPY+1*(HEIGHT+V_SPACE);  
61for(int i=0;i<specialButton.length;i++)  
62{  
63specialButton[i]=new MySpecialButton(tempX,y,WIDTH*2,HEIGHT,specialButtonText[i], this);  
64specialButton[i].setForeground(Color.RED);  
65tempX=tempX+2*WIDTH+H_SPACE;  
66}  
67  
68//set Co-ordinates for Digit Buttons  
69int digitX=TOPX+WIDTH+H_SPACE;  
70int digitY=TOPY+2*(HEIGHT+V_SPACE);  
71tempX=digitX;  y=digitY;  
72for(int i=0;i<digitButton.length;i++)  
73{  
74digitButton[i]=new MyDigitButton(tempX,y,WIDTH,HEIGHT,digitButtonText[i], this);  
75digitButton[i].setForeground(Color.BLUE);  
76tempX+=WIDTH+H_SPACE;  
77if((i+1)%3==0){tempX=digitX; y+=HEIGHT+V_SPACE;}  
78}  
79  
80//set Co-ordinates for Operator Buttons  
81int opsX=digitX+2*(WIDTH+H_SPACE)+H_SPACE;  
82int opsY=digitY;  
83tempX=opsX;  y=opsY;  
84for(int i=0;i<operatorButton.length;i++)  
85{  
86tempX+=WIDTH+H_SPACE;  
87operatorButton[i]=new MyOperatorButton(tempX,y,WIDTH,HEIGHT,operatorButtonText[i], this);  
88operatorButton[i].setForeground(Color.RED);  
89if((i+1)%2==0){tempX=opsX; y+=HEIGHT+V_SPACE;}  
90}  
91  
92addWindowListener(new WindowAdapter()  
93{  
94public void windowClosing(WindowEvent ev)  
95{System.exit(0);}  
96});  
97  
98setLayout(null);  
99setSize(FRAME_WIDTH,FRAME_HEIGHT);  
100setVisible(true);  
101}  
102//////////////////////////////////  
103static String getFormattedText(double temp)  
104{  
105String resText=""+temp;  
106if(resText.lastIndexOf(".0")>0)  
107    resText=resText.substring(0,resText.length()-2);  
108return resText;  
Clelia
21 May 2020
1/* Create Switch cases */
2Scanner sc= new Scanner(System.in);
3int firstNum= sc.nextInt();
4int secondNum= sc.nextInt();
5System.out.println("Enter an operator (+, -, *, /):");
6char operator= sc.next().chartAt(0);
7double result=0;
8switch(operator){
9  case "+":
10    result+= firstNum+ secondNum;
11    break;
12  case "-":
13    result+= firstNum-secondNum;
14    break;
15  case "*":
16    result+= firstNum*secondNum;
17    break;
18  case "/":
19    result+= firstNum/secondNum;
20    break;
21}
Celina
07 Sep 2016
1package main;
2
3import java.util.Scanner;
4
5public class calculator {
6
7    public static void greetings(){
8        System.out.println("Hi welcome to the calculator made by Pepa pig made in java! :D");
9    }
10
11    public static void instruction(){
12        System.out.println("Hi welcome to the calculator made by Pepa pig made in java! :D");
13    }
14
15    public static void calculator(){
16        Scanner keyboardInput = new Scanner(System.in);
17        String calculator;
18
19        do {
20            System.out.print("Enter your first digit: ");
21            double num1 = keyboardInput.nextDouble();
22
23            System.out.print("Enter your operator: ");
24            String op = keyboardInput.next();
25
26            System.out.print("Enter your second digit: ");
27            double num2 = keyboardInput.nextDouble();
28
29            System.out.print("= ");
30
31            if (op.equals("+")){
32                System.out.print(num1 + num2);
33                System.out.println("\nYou were adding");
34                System.out.println("How I know that cause I have big brain and a smart programmer ;)");
35            }else if (op.equals("-")){
36                System.out.print(num1 - num2);
37                System.out.println("\nYou were subtracting");
38                System.out.println("How I know that cause I have big brain and a smart programmer ;)");
39            }else if (op.equals("*")){
40                System.out.print(num1 * num2);
41                System.out.println("\nYou were multiplying");
42                System.out.println("How I know that cause I have big brain and a smart programmer ;)");
43            }else if (op.equals("/")){
44                System.out.print(num1 / num2);
45                System.out.println("\nYou were dividing");
46                System.out.println("How I know that cause I have big brain and a smart programmer ;)");
47            }else if (op.equals("%")){
48                System.out.print(num1 % num2);
49                System.out.println("\nYou were remanding");
50                System.out.println("How I know that cause I have big brain and a smart programmer ;)");
51            }else{
52                System.out.println("Invalid operator");
53            }
54            System.out.print("Would you like to restart the calculator? (y or n): ");
55            calculator = keyboardInput.next();
56        }
57        while (calculator.equals("y"));
58
59        if (calculator.equals("n")){
60            System.out.println("Leave then kiddo ;-;");
61        }
62    }
63
64
65    public static void main(String[] args) {
66        greetings();
67        instruction();
68        calculator();
69    }
70}
Nikki
29 Mar 2017
1import java.util.Scanner;
2
3public class Calculator {
4
5    public static void main(String[] args) {
6
7        Scanner reader = new Scanner(System.in);
8        System.out.print("Enter two numbers: ");
9
10        // nextDouble() reads the next double from the keyboard
11        double first = reader.nextDouble();
12        double second = reader.nextDouble();
13
14        System.out.print("Enter an operator (+, -, *, /): ");
15        char operator = reader.next().charAt(0);
16
17        double result;
18
19        switch(operator)
20        {
21            case '+':
22                result = first + second;
23                break;
24
25            case '-':
26                result = first - second;
27                break;
28
29            case '*':
30                result = first * second;
31                break;
32
33            case '/':
34                result = first / second;
35                break;
36
37            // operator doesn't match any case constant (+, -, *, /)
38            default:
39                System.out.printf("Error! operator is not correct");
40                return;
41        }
42
43        System.out.printf("%.1f %c %.1f = %.1f", first, operator, second, result);
44    }
45}
queries leading to this page
java operator how to make an calculatormaking a simple calculator in javacalculaor javacalculator in java using switchhow to write calculator program in javamake calculator using oops in javacreate a class calculator in javacreating a calculator with java calculator javacalculator on javahow to make a simple calculator in java using methodscalculate with javacalculator using scanner method in javacode calculator javacalculator java codecreate a simple calculator javacalculator using java classmake calculator using switch case in javajava calcuator codejava calc functonjava bmi calculatorhow to make simple cacultor in javajava calculatprbuilding a calculator using javahow to create a functioing calculator in javaimplement calculator in javabasic calculator javahow to make calculator in jaadesign a simple calculator using switch components using 2 text fieldsjava calculator app codesimple calculator in java using switch casecalc javacreate calculator in javasimple calculator operators program with same method name in javacalculator avec switch in javasimple calculation in javasimple java calculator in java calculator javajava 25 calculator 21 java calculator program needed a classcalbulator program in javacreate calculator app in javacalculator in java entter any numberhow to make a basic calculator in javacalculator java codecode for calculator javajava calculator code examplecalculator program in java using switch casecalc 28 javajava function calculator codecalculator program in javajava program for calculator using switch casebasic java calculatorbasic calculator program in javacomputer programing code for a calculator in javacalculator coding in javajava calculator switch caseimplement a simple calculator javajava calculator program codejava comparatorcalculator program in java using methodsswitch case java use for calculatorbuilding a calculator in javahow to make calculator app in javacreate a basic calculator in javacode for calculator javascalculator with javahow to make a calculator in jaasimple calculaotr javajava calculatermake a simple calculator in javahow to make java calculatorjava big o calculatorin java calculator programcalculator in javacalculator example in javajava program where the user chooses his operation without case how to code a calculator in javasimple caulcotr with javacalculator using scanner in javajava how to make calculatorbuilding aa basic calculator javajava program to display basic calculator using switch statement how to make in calculator x 5ey javacode java calculatricesimple calculator design in javajava calculator appjava calculator operatorssimple calculator program in javajava code to create a calculatorjava program to implement basic calculator operationsjava calculation 22calculator java 22calculator java source codejava program to make a calculatorcalculator code javaprogram in java to create a calculator with methods in javahow calculator works in jaswitch case calculator in javahow to make calculator in javain java calculator program needed a classsimple java calculatorbasic java calculator using scanner 25 calculator in javaprogram for making calculator in javajava how to use multiple calculator operations in one screenjava calassesjava calculator onlineprogram to make a simple calculator in javajava calculator filejava calculator program using methodsjava code for calculationcreate a simple calculator in javaexamples of calculator javawriting a calculator in javahow to write a calculator in javahow to covert 0721501212 to 2b27721501212 in javacalculator code for javajava code calculatorcalculatro javamake a simple calculator using a string in javacalculator app java point functioncalculator using javacalculator code in javaimplement calculator application with add 2c divide 2c multiply and subtract methods full calculator program on javacreate a java calculatorcalcultor in javajava simple calculator with methodswhat does calc do in javacalculator clas in java simple calculator javajava calculator programjava program to create a calculatorwrite a simple calculator program in javajava program to make a simple calculator using switch casecalculator in java tutorialcaculator javausing switch case how to create calculatorin javacalculator javajava tutorial calculatorcreate calculator using javajava example calculatorcalcullator java program examplejava program to create a calcualtor how to use methods to make a calculator in javacalculator with switch case in javacomplete calculator in javajava caculator example code 5ccreate a calculator class that has following methods 3a sum 2c multiply 2c divide 2c modulus 2c sin 2c cos 2c tan the user should be able to call these methods without creating an object of calculator class program in javabasic calculator code in javajava calc with switchcacert javasimple calculator program in java source codejava calculator codehow to program a calculator with javajava code to create a simple calculatorcalculator in javaswingcalculator app in javajava calculatrocalculator java program in javacalculator in java how to program a simple calculator in javacalculator simple program in javacalcultor using javajava code for simple calculatorhow to build calculator in javasimple caculator javacalculator java switch caseprogram a calculator in javajava switch case calculatorhow to covert 0721501212 to 27721501212 in javacreate a calculator javadesign a simple calculator using switch componentsmake calculator in javamake calculator using javacalculator in javsmaking calculator in javahow to create calculator in javahow to make ajava statistics calculatorcase switch java calculatorcreating calculator in javacreate a simple calculator application which asks a user to enter two numbers and the operation 28e g 2b 2c 2c x 2c etc 29 that they e2 80 99d like to perform on the numberscalculator java classesmaking calculator with javacalculator program with exit code javajava switch statement add subtract dividejava calculatecalculatorsys javajava method calculatorsimple calculator in javawrite a program in java to exhibit the functionality of a calculator java calculator simplewe have to create a calculator and take input from the user that which operation perform like addition subtraction multiplication by choosing an operation should be perform using scanccer method javajava calculator using switch casecalculator function in javajava program calculations examplehow to make a calculator using javaalgorithm for building multiple arithmetic operation calculator with javacalculator in java source codecreate a basic calculator jacasimple calc in javajava program that calculate create a simple calculator using javacalcultor example in javajava class method calculatorcalculator using switch case and do while in javacalculator java program using switchjava code for calculator in notepadmake a calculator using switch statement in javajava solvercode for java calcullatorprogram to create calculator in javajava create calculatorhow to put 25 in a calculator using javacalculator program with calulate method in javaswitch case in java calculatorwhat does java use to calculatewhow to make java calculatorsimple calculator program in java using switch casehow to make calculater in javacreate a simple calculator taking input as stringa program of calculator in javaarithmetic calculator java programwrite a program to create calculator in java must contain mfollowing method input vales 2cadd 2csub 2cdivide 2cmultiply and having to clases to implement these methodjava program of calculator java calculatorjava calculator switch case loophow to create a calculator in javajava calculatorcalculator program using switch case in javabuilding calculator program in javacalculadora javamake a calculator in javajava function calculatorcalculater in java how to make calculator javajava simple calculatoraqi calculator javacalculator in java using switch case in loopjava calculator projectsimple calculator javascript github vs codejava program for creating calculatorbuild a calculator in javajava calculatowrite a program to create calculator in java must contain mfollowing method input vales 2cadd 2csub 2cdivide 2cmultiply and having two clases and method overloadingjava simple calculator programwe have to create a calculator and take input from the user that which operation perform like addition subtraction multiplication by choosing an operation should be perform using switch statement in javacalculatrice javajava calculator program using switch casea simple calculator in javacalculator using switch case in javajava make caculatorcreate calculator javawrite a java program to perform basic calculator operations how to program a calculator in javacalculator kjavawindows calculator frontend development in javabig o calculator javacalculator class in javacode for calculator in java java program for calculatorcalculator java programhow to make a caculator in javacreate simple calculator java classescalculator in java using switch casejava project calculatorcreating a calculater in javahow to build a simple calculator using javajava code for calculatorbasic java calculator programcode to creat a simple calculator using javacalculator 2f in javacreate a calculator using switch case in javacalculator for java mathsimple calculator command in javacalculator in javasimple calculator in java without switch casejava make a calculatorjava program to make a simple calculatorsteps in building a calculator in javacode in java for calculatornumber manipulation programs in javajava codes for calculatorlogic of calculator program of many numbers in javacalculator logic in javacalculator app javacreate a calculator in javascientific calculator in java using switch casehow to calculate in java programinghow to make a java calculatorwe have to create a calculator and take input from the user that which operation perform like addition subtraction multiplication by choosing an operation should be performed using scanner method javahow to make a calculator javamaking a calculator in javahow to amke a calcualtor in javasimple caclater in javahow to make a simple calculator in javahow to make a calculator using switch case in javahow to make a calculator in javacreate simple calculator javacalaculator in java codecalculator app using javahow to build a calculator in javajava make calculatorexample program for calculator in javajava program to make calculatorjava calculator classmake a simple calculator using stringsimple calculator java programjava calculator source codecalculator program in java source codewhat is the code of calculator in javabasic calculator in javabasic calculation codes javajava program calculatordesign a simple calculator using switch components using 2 text fields inputhow to calculate are in java full codecalculator with add subtract methods javasimple calculator in core javajava program make calculatorwrite a program to create calculator in java must contain 5 methods and two java classes how to make a simple java calculatorhow to build a calculator with javasimple calculator java source codecalculator program in java