Basic Mathematical Operations 8 kyu: Codewars Solutions

Basic Mathematical Operations

Description

Your task is to create a function that does four basic mathematical operations.

The function should take three arguments – operation(string/char), value1(number), value2(number).
The function should return result of numbers after applying the chosen operation.

Examples(Operator, value1, value2) –> output

('+', 4, 7) --> 11
('-', 15, 18) --> -3
('*', 5, 5) --> 25
('/', 49, 7) --> 7

Solution

public class BasicOperations
{
  public static Integer basicMath(String op, int v1, int v2)
  {
    int result = 0;
    if (op.equals("+")) {
    result = v1 + v2;
    }
    if (op.equals("-")) {
    result = v1 - v2;
    }
    if (op.equals("*")) {
      result = v1 * v2;
    }
    if (op.equals("/")) {
      result = v1 / v2;
    }
    return result;
  }
}
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments