Coding Bat – sumDouble

This exercise requires that the program return the sum of two provided numbers. If the two provided numbers are the same, the sum is to be doubled.

Here is the code that you start with:

public int sumDouble(int a, int b) {
  
}

As you can see, we have two integers (a and b) that have been declared.

public int sumDouble(int a, int b) {
  int sum = a + b;
  if (a == b) {
    sum = sum * 2;
  } 
  return sum;
}

In this solution, we have created an integer variable named “sum” which has the value of “a” added to “b”.

We then move to a conditional statement. If “a” equals “b”, the variable “sum” is multiplied by 2. We only enter this instruction if “a” and “b” are equal to each other.

Following either entry of the IF statement or bypassing it, we display the value of the variable “sum”.

Leave a Reply

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