I am trying to figure out a way to write a program that takes the "digital product" of a number (has to be positive). Like if I had 123456789, it would take 1*2*3*4*5*6*7*8*9 which is 362880. It would then take 3*6*2*8*8 (leaves out zeros) which is 2304. Then 2*3*4 which is 24. Then 2*4 which is 8. So it would print out "123456789 --> 362880 --> 2304 --> 24 --> 8". I have done this iteratively:
public class DigProd
{
public static void main(String args[ ])
{
long a = 123456789;
int ai = DigProdIterative(a);
System.out.println("Digital Product of a: " + ai);
int ar = DigRootRecursive(a);
System.out.println("Digital Product of a: " + ar);
}
public static int DigProdIterative(long a)
{
String str = String.valueOf(a);
int first = 1;
int y = (int) a;
while(str.length() != 1)
{
str = String.valueOf(y);
if(str.length() == 1)
{
System.out.print(y + "\n");
}
else
{
System.out.print(y + " --> ");
}
y = 1;
for(int i = 0; i < str.length(); i++)
{
int b = str.charAt(i);
b = b - 48; //for ascii code
if(b != 0)
{
first = (int) str.charAt(i);
first = first - 48;
y = y * first;
}
}
}
return y;
}
but I now need to do it recursively:
public static int DigRootRecursive(long x)
{
for()
{
}
}
Someone please help me with this! I have tried doing it on my own but this is too hard!! PLEASE HELP!!