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!!

You could take the long and write it to a string, eg

ling a =  1234567;
String s = a;

Next use substring to get the various digits out, eg

String mySubString = s.substring(0, 1);
int newInt = mySubString;

You could then work with the number as you like. This only will work if you are certain of the number of digits. Failing that you will have to get the length of the string and use an array.

Well how would I use all that and make it recursive???? I am really trying and if you could just write the recursive part, it would make my week!! I normally do not try to get others to do my homework, but I am in a real bind. Please find it in you to help me!!!

The idea with using String is good

String mySubString = s.substring(0, 1);
int newInt = mySubString;

but you forgot to parse Integer int newInt = Integer.parseInt(mySubString) . Yes I know this work but it is bad habit. Why did SunMicrosystem made the method parseInt()? Just to have one extra method?. It is supposed to be used. So please use it...:cheesy:

To work recursively, here is what you need

for(int i = str.length(); i > 0; i--)
{
	int num = Integer.parseInt(str.substring(i-1, i));
        // other operation => add
}

I am not really following you. What does this mean:

// other operation => add

What ever you need to do with your number which you just get it from string...

Okay....I need to multiply the first and second number....how do I get it from the string? Then I add it? Help!

Sorry..need to add to my question....How would getting something (the String) that I made in the DigProdRecurisve be recursive? The "123456789" is a long not a String. I was under the impression that to make it recursive, I would have to call DigProdRecursive somewhere. Like

public static int DigProdRecursive(long x)
{
   return DigProdRecursive;
}

try this function:
package com.Thread;
public class DigProd {
public static void main(String args[]) {
long a = 123456789;
long ai = DigProdIterative(a);
System.out.println("Digital Product of a: " + ai);
long ar = Recursive(a);
System.out.println("Digital Product of a: " + ar);
}

public static long doNumRecursive(long a){
long first,y=1;
String str = String.valueOf(a);
if(str.length()==1){
return a;
}
for (int i = 0; i < str.length(); i++) {
int b = str.charAt(i);
b = b - 48; // for ascii code
if (b == 0) {
continue;
} else {
first = str.charAt(i);
first = first - 48;
y = y * first;
}
}
return y;
}

public static long Recursive(long result){
String str = String.valueOf(result);
if(str.length()==1){
System.out.print(result + "\n");
return result;
}else{
System.out.print(result + " --> ");
result=doNumRecursive(result);
str = String.valueOf(result);
if(str.length()==1){
System.out.print(result + "\n");
return result;
}else{
result = Recursive(result);
return result;
}
}
}

public static long DigProdIterative(long a) {
String str = String.valueOf(a);
int first = 1;
long y = 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) {
continue;
} else {
first = str.charAt(i);
first = first - 48;
y = y * first;
}
}
}
return y;
}
}:rolleyes:

if (b == 0) {
continue;
} else {

is continue needed ?
It will work well without that.

deng_cen......... I bow down. You are awesome. Thank you very much.

Member Avatar for iamthwee

He's not awesome, he has merely helped you to stay on your course for another week.

Let's see what you do when your teacher rolls out the next assignment?

Let's keep it pleasant guys, before the infraction stick comes out.

publicclass Recursion {
static long st = 440403;
static int no = 0;
static int count = 1;
static int finalVal = 1;

/**
* @param args
*/
public static void main(String[] args)throws Exception {
no = String.valueOf(st).length();
try {
System.out.println("final "+doRecursive(st));
} catch(Exception e){
e.printStackTrace();
}

}
static long doRecursive(long x){
String str = String.valueOf(x);
count = count++;
if( count != no ) {
int i = (int)str.charAt(0);
System.out.println(i);
i = i - 48;
if( i == 0 ) i = 1;
finalVal = finalVal * i;
str = str.substring(1,str.length());
if(str.equals(""))return finalVal;
x = Long.parseLong(str);
return doRecursive(x);
}

return finalVal;

}

}

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.