Click here to Skip to main content
15,891,529 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Define a method which returns the sum of three rounded numbers. If the right most digit of the number is lessthan 5, then round off it's value to the previous multiple of 10 otherwise if the right most digit of the number is greater or equal to 5, then round off to the next multiple of 10.


What I have tried:

public class Roundofff {
public static void main(String[] args) {
		        int a = 23, b = 34, c = 69;
		        System.out.println(sumOfMultiples(a, b, c));
		    }

		    public static int sumOfMultiples(int a, int b, int c) {
		    	if(a <= 0 && b <= 0 && c <= 0){
		    		return -1;
		    	}
		    	a = a + (10 - a % 10);
		    	b= b + (10 - b % 10);
		    	c= c + (10 - c % 10);
		    	
		    return a + b +c;
		}
}
Posted
Updated 20-Oct-17 21:36pm

1 solution

Try using a function to round numbers:
C#
public static int Round(int x)
   {
   int leastDigit = x % 10;
   return (x - leastDigit) + (leastDigit < 5 ? 0 : 10);
   }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900