Java > Logic-2 > roundSum (CodingBat Solution)

Problem:

For this problem, we'll round an int value up to the next multiple of 10 if its rightmost digit is 5 or more, so 15 rounds up to 20. Alternately, round down to the previous multiple of 10 if its rightmost digit is less than 5, so 12 rounds down to 10. Given 3 ints, a b c, return the sum of their rounded values. To avoid code repetition, write a separate helper "public int round10(int num) {" and call it 3 times. Write the helper entirely below and at the same indent level as roundSum().

roundSum(16, 17, 18) → 60
roundSum(12, 13, 14) → 30
roundSum(6, 4, 4) → 10


Solution:

public int roundSum(int a, int b, int c) {
  return round10(a) + round10(b) + round10(c);
}

public int round10(int n) {
  if (n % 10 < 5)
    return n - (n%10);
  else
    return n + (10 - (n%10));
}


8 comments :

  1. Can you explain the returns? (New to coding)
    "return n - (n%10);
    else
    return n + (10 - (n%10));
    "

    Thanks

    ReplyDelete
  2. public int roundSum(int a, int b, int c) {
    return round10(a) + round10(b) + round10(c);
    }

    public int round10(int num){
    int roundtomod = num%10;
    /*
    // 16%10=6 6>5 n=20
    if (roundtomod >= 5 ) {return num + (10 - roundtomod); } //16 + (10-6)=16+4 = 20
    //12%10=2 2<5 n=10
    return num - roundtomod; // 12-2 = 10
    */
    //optimize
    return (roundtomod >= 5)?(num + (10 - roundtomod)):(num-roundtomod);
    }

    ReplyDelete
  3. int sum = 0;
    sum += round10(a);
    sum += round10(b);
    sum += round10(c);

    return sum;

    }


    public int round10(int num){
    int tmp = 0;
    if(num%10>=5){
    num = (num - num%10) + 10 ;
    }
    else{
    num = num- num%10;
    }
    return num;

    ReplyDelete
  4. public int roundSum(int a, int b, int c) {
    return round10(a) + round10(b) + round10(c);
    }

    // Helper function to round a value based on its last digit.
    public int round10(int num) {
    int remainder = num % 10;
    num -= remainder;
    if (remainder >= 5) {
    num += 10;
    }
    return num;
    }

    ReplyDelete
  5. public int roundSum(int a, int b, int c) {

    return round10(a) + round10(b) + round10(c);
    }
    public int round10(int num) {

    if (num % 10 < 5)
    {
    return (num / 10) * 10;
    }
    else
    {
    return ((num / 10) + 1) * 10;
    }
    }

    ReplyDelete
  6. public int roundSum(int... nums) {
    int sum=0;
    for(int n:nums){
    int rem=n%10;
    if(rem>=5)
    n=n+(10-rem);
    else
    n=n-rem;

    sum+=n;
    }
    return sum;
    }

    ReplyDelete
  7. public int roundSum(int... ar) {
    int sum=0;
    for(int a:ar){
    a=a%10>=5?((a/10)+1)*10:((a/10))*10;
    sum+=a;
    }

    return sum;
    }

    ReplyDelete
  8. public int roundSum(int a, int b, int c) {
    return round10(a) + round10(b) + round10(c);
    }
    public int round10(int num) {
    return (num%10 >= 5) ? num + (10 - num%10): num - (num%10);
    //if it's true, calculate what's the right digit (via mod 10) and subtract it from 10 (since we are dealing with mutliples of 10 and we need to find how many to get to the next multiple of 10)
    //afterwards, add it to the num to increase to the next multiple of 10.
    //if false, find the right digit via mod 10 and subtract it from the num to round the num down to OG multiple.
    }
    Yerr me

    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