A simple puzzle to share with today...

Write a program to find sum of all integers of a given number such that sum is always a single digit.

For instance if the given number is 6789, then answer should be 6+7+8+9 = 30 and then 3+0 =3.

A straight forward logic is to write a recursive function which calculates sum of digits until the it is less than 10. But there an efficient logic than this. Here is that

1. Calculate mod(input_integer,9)

2. if the result is 0 then

sum of digits reduced to single digit is 9

else

value returned in step 1 is the sum of digits of input integer reduced to 9.

Write a program to find sum of all integers of a given number such that sum is always a single digit.

For instance if the given number is 6789, then answer should be 6+7+8+9 = 30 and then 3+0 =3.

A straight forward logic is to write a recursive function which calculates sum of digits until the it is less than 10. But there an efficient logic than this. Here is that

1. Calculate mod(input_integer,9)

2. if the result is 0 then

sum of digits reduced to single digit is 9

else

value returned in step 1 is the sum of digits of input integer reduced to 9.

Here is my oracle SQL solution,

SELECT DECODE(MOD(INPUT_INTEGER,9),0,9,MOD(INPUT_INTEGER,9))SELECT DECODE(MOD(INPUT_INTEGER,9),0,9,MOD(INPUT_INTEGER,9))

**FROM DUAL;**