Competitive Programming Solutions

GitHub
back to dashboard

O. Calculator

Codeforces - Sheet #1 (Data type - Conditions) · limit: 1s · memory: 256MB · view on codeforces

input

7+54

output

61
Main.java
static void solve() {

        // Write your solution here
        String input = in.next();
        int i = 1;
        while (input.charAt(i) != '+' && input.charAt(i) != '-' && input.charAt(i) != '*' && input.charAt(i) != '/') {
            i++;
        }
        int x = Integer.parseInt(input.substring(0,i));
        char c = input.charAt(i);
        int y = Integer.parseInt(input.substring(i + 1));
        int result = 0;
        switch(c){
            case '+': result = x + y;
            break;
            case '-': result = x - y;
            break;
            case '*': result = x * y;
            break;
            case '/': result = x / y;
            break;
        }
        out.println(result);
    }