Write a java program to create math calculator.

Write a program that accepts three inputs, first two inputs are operands in int form and the third one being one of the following five operators: +, -, *, /, %. Implement calculator logic and return the result of the given inputs as per the operator provided. In case of division, Assume the result would be the integer.
Input and Output Format:
Input consists of two integers and a character.
The output consists of an integer.
Refer sample output for formatting specifications.

Sample Input 1:
23
2
*
Sample Output 1:
46

Math calculator in Java.

import java.util.Scanner;

public class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int a = sc.nextInt();
    int b = sc.nextInt();
    char c = sc.next().charAt(0);
    int d = display(a, b, c);
    System.out.println(d);
  }

  public static int display(int a, int b, char c)

  {
    int a1 = 0;
    if (c == '*') {
      a1 = a * b;
    }

    else if (c == '+') {
      a1 = a + b;
    } else if (c == '-') {
      a1 = a - b;
    } else if (c == '/') {
      a1 = a / b;
    } else if (c == '%') {
      a1 = a % b;
    }

    return a1;
  }
}

Add a Comment

Your email address will not be published. Required fields are marked *

5 × 2 =