##
**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)); }

Can you explain the returns? (New to coding)

ReplyDelete"return n - (n%10);

else

return n + (10 - (n%10));

"

Thanks

public int roundSum(int a, int b, int c) {

ReplyDeletereturn 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);

}

int sum = 0;

ReplyDeletesum += 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;

public int roundSum(int a, int b, int c) {

ReplyDeletereturn 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;

}

public int roundSum(int a, int b, int c) {

ReplyDeletereturn 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;

}

}

public int roundSum(int... nums) {

ReplyDeleteint 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;

}

public int roundSum(int... ar) {

ReplyDeleteint sum=0;

for(int a:ar){

a=a%10>=5?((a/10)+1)*10:((a/10))*10;

sum+=a;

}

return sum;

}

public int roundSum(int a, int b, int c) {

ReplyDeletereturn 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