1

Suppose

int n = 5

I want to find out the sum of n number. For example:- if n is given by user as 5 then in number array from 1 to 30 we have sum of n number is 14 and 23. Which is int number = 1+4= 5 and 2+3 = 5 that is number == n. To reduce double digits i.e., if user put 15 then:-

reduce(int doubleDigit){
return (doubleDigit-1) %9 +1;

So to reduce 15 i.e., 1+5=6

But to calculate the sum of 6, how to find? What I want as a result is that when ans is 6 then the output should be 6, 15 (1+5=6) and 24 (2+4=6) which is sum of two digits is equal to ans in the range between 1 to 30.

So do I need to reverse the reduce function or is their any method to solve.

Small code hint will be very helpful.

1 Answers1

1

There is a semi-tautological function for doing this:

public int sumDigits (int num) {
  int sum = 0;
  while (num > 0) {
    sum += num % 10;
    num /= 10;
  }
  return sum;
}
ControlAltDel
  • 33,923
  • 10
  • 53
  • 80