Java > Warmup-1 > sumDouble (CodingBat Solution)

Problem:

Given two int values, return their sum. Unless the two values are the same, then return double their sum.

sumDouble(1, 2) → 3
sumDouble(3, 2) → 5
sumDouble(2, 2) → 8


Solution:

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


1 comment :

  1. public int sumDouble(int a, int b) {
    return (a == b)? 4 * a : a + b;
    }

    ReplyDelete

Follow Me

If you like our content, feel free to follow me to stay updated.

Subscribe

Enter your email address:

We hate spam as much as you do.

Upload Material

Got an exam, project, tutorial video, exercise, solutions, unsolved problem, question, solution manual? We are open to any coding material. Why not upload?

Upload

Copyright © 2012 - 2014 Java Problems  --  About  --  Attribution  --  Privacy Policy  --  Terms of Use  --  Contact