Search This Blog

Friday, May 28, 2021

Calculator source code (java)

 Calculator


import java.util.Scanner;


public class Calculator {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        int a1 = 1;

        while (a1 == 1){

        System.out.print("Enter 1st no: ");

        float input1 = sc.nextFloat();

        System.out.print("Enter the operation: ");

        String input = sc.next();

        System.out.print("Enter 2nd no: ");

        float input2 = sc.nextFloat();

        String a = new String("+");

        String b = new String("-");

        String c = new String("*");

        String d = new String("/");

        System.out.print("Result is: ");

        if (input.equals(a)){

            System.out.println(input1+input2);

        }

        else if (input.equals(b)){

            System.out.println(input1-input2);

        }

        else if (input.equals(d)){

            System.out.println(input1/input2);

        }

        else if (input.equals(c)){

            System.out.println(input1*input2);

        }

else {

            System.out.println("Invalid operation");

        }

           System.out.println("");

        }

    }

}


The above Java code can be used in a GUI to make it look good and user friendly.