calculatorBasic.java

less than 1 minute read

This program was created to imitate a basic calculator. It’s more of a fake calculator at this point.

It contains preset conditions to check which operation is being performed and performs that operation and returns the result.

The code has been modified slightly and now uploaded to the site.

<— Return Home

Code Snippet

// Link of Edabit: https://edabit.com/challenge/gyfsGx7KrGLscxFrD
public class calculatorBasic { 
    public static void main(String[] args){
        int result = calculator(2, '+', 2);
        int result2 = calculator(2, '*', 2);
        int result3 = calculator(4, '/', 2);
        System.out.println(result);
        System.out.println(result2);
        System.out.println(result3);
    }
    
    public static int calculator(int num1, char operator, int num2){
        int operation = 0;
        if (operator == '+'){
            operation = num1 + num2;
        }
            else if (operator == '*'){
                operation = num1 * num2;
            }
                else if (operator == '/'){
                    operation = num1 / num2;
                }
                    else if (operator == '-'){
                        operation = num1 - num2;
                    }
        return operation;
    }
}

<— Return Home