DEV Community

Aafthab Ali Shaik
Aafthab Ali Shaik

Posted on

more efficient Calculator in Java

the code!!

import java.util.Scanner;
public class Main
{
    public static int sum(int num1,int num2){
        return num1+num2;
    }
    public static int sub(int num1,int num2){
        return num1-num2;
    }
    public static int mul(int num1,int num2){
        return num1*num2;
    }
    public static int div(int num1,int num2){
        return num1/num2;
    }
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        System.out.println("\nEnter the First number:");
        int a = sc.nextInt();
        System.out.println("\nEnter the Second number:");
        int b = sc.nextInt();
        System.out.println("\n Enter 1 of Addition\n Enter 2 of Subtraction \n Enter 3 for Multiplication \n Enter 4 for Divison:");
        int c = sc.nextInt();
        int sumresult = sum(a,b);
        int subresult = sub(a,b);
        int mulresult = mul(a,b);
        int divresult = div(a,b);
        if (c==1){
            System.out.println("The Addition of " +a+ " and " +b+ " is " +sumresult);
        }
        else if(c==2){
            System.out.println("The Subtraction of "+a+ " and "+b+" is "+subresult);
        }
        else if(c==3){
            System.out.println("The Multiplication of "+a+" and "+b+ " is "+mulresult);
        }
        else if(c==4){
            System.out.println("The Divison of "+a+" and "+b+" is "+divresult);
        }
        else
        while(c<4||c<=0){
            System.out.println("Please Enter the Number between 1 and 4:");
             c = sc.nextInt();
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)