0

I am writing a simple code in Java that is using recursion. I want to show the product of two numbers that a user will enter. I managed to do that using recursion, but stuck at the point where I want to show that the product could be written as (example) 10*5 = 5+5+5+5+5+5+5+5+5+5 (10 times), or 12*3 = 3+3+3+3+3+3+3+3+3+3+3+3 (12 times). Here is my code so far. In the code i put a comment where it should be written (example). Thanks.

import java.util.Scanner;

public class RecursiveMultiplication {

public static void main(String[] args) {
    Scanner key = new Scanner(System.in);
    int a, b;
    System.out.print("Enter first number: ");
    a = key.nextInt();
    System.out.print("Enter second number: ");
    b = key.nextInt();
    System.out.println("The product of " + a + " and "
            + b + " is: " + multiRec(a, b));
    System.out.println("It could also be written as: ");   //Here should product be broken into smaller numbers


}

public static int multiRec(int x, int y) {
    if (x == 0 || y == 0) {
        return 0;
    } else {
        if (x == 1) {
            return y;
        } else {
            return x + (multiRec(x, y - 1));
        }
    }

  }

}
2
  • Is there a specific problem that you are encountering with the code? Commented Mar 3, 2016 at 4:33
  • The problem is that I don't know how to break the product into smaller numbers and show it to a user. As I said in the example (10*5 = 50). Now, I want to write that product as 5+5+5+5+5+5+5+5+5+5 on the screen Commented Mar 3, 2016 at 4:52

2 Answers 2

3

A StringBuilder should be defiend as

StringBuilder buf = new StringBuilder (a);

Pass this StringBuilder paramater into multiRec

and then change multiRec to be

public static int multiRec(int x, int y, StringBuilder buf) {
    if (x == 0 || y == 0) {
        return 0;
    } else {
        if (x == 1) {
            return y;
        } else {
            buf.append (" + ").append (x);
            return x + (multiRec(x, y - 1, buf));
        }
    }

  }

}

Then when completed simply printout its value

Sign up to request clarification or add additional context in comments.

Comments

0
    import java.util.Scanner;

public class RecursiveMultiplication {
    public static void main(String[] args) {
        Scanner key = new Scanner(System.in);
       int a , b;
        System.out.print("Enter first number: ");
        a = key.nextInt();
        System.out.print("Enter second number: ");
        b = key.nextInt();
        System.out.printf("%d %s %d %s",a , "*" , b ,"= ");
        System.out.println("\nThe product of " + a + " and "
                + b + " is: " + multiRec(b, a));
       // System.out.println("It could also be written as: ");   //Here should product be broken into smaller numbers


    }

    public static int multiRec(int x, int y) {

        if (x == 0 || y == 0) {
            return 0;
        } else {
            System.out.print(x+" ");
            if (y == 1) {
                return x;
            } else {
                System.out.print(" + ");
                return x + (multiRec(x, y - 1));
            }
        }

      }
}

3 Comments

Both examples helped me to understand how to achieve what I need. The only thing is formatting. In both cases there is a + sign at the end of the last smaller number. Here is what I mean by that. Output: Enter first number: 10 Enter second number: 5 5 + 5 + 5 + 5 + 5 + 5 + 5 + 5 + 5 + 5 + The product of 10 and 5 is: 50
In the above code , I have already taken care about the last "+" symbol.
Sorry, you did. I appreciate your help. I found different problem now when i put 10 as first number and 5 as second number, here is the output : Enter first number: 10 Enter second number: 5 10 * 5 = 5 + 5 + 5 + 5 + 5 + 5 + 5 + 5 + 5 + 5 The product of 10 and 5 is: 46 (Don't know why it is 46) Also, i tried to write "The product of 10 and 5 is 50" and then below that "It could be also written as: 5+5+5+5+5+5+5+5+5+5.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.