How to Multiply Two Positive Integers Using Recursion | Comp Sci Questions

  • Thread starter courtrigrad
  • Start date
  • Tags
    comp sci
In summary, the conversation discusses writing a recursive method to multiply two positive integers using repeated additions. The correct code for the method is provided and the correct output for a given input is determined. There is also a correction made for a mistake in the code.
  • #1
courtrigrad
1,236
2
Hello all

If we want to write a recursive method that multiplies two positive integers using repeated additions, we know that: [tex] a\times b = a + (a\times(b-1)) [/tex] Would this be correct:

Code:
// pre: a and b are positive
// post: returning the product of a and b.
public int mult(int a, int b )

a*b = a +(a * (b-1))
if( b ==1)
return(a);
else
  return mult(a + a*(b-1)))


Also if you had:

Code:
public void hello(int n )
{
   System.out.println("hello n = ",n);
   if(n > 1)
      hello(n-1);
   System.out.println("goodbye n = ", n);

How would you find the output when a 3 is passed? I got a bumch of hellos and goodbyes, but I got it wrong


Thanks :smile:
 
Physics news on Phys.org
  • #2
are these right?
 
  • #3
courtrigrad said:
Code:
// pre: a and b are positive
// post: returning the product of a and b.
public int mult(int a, int b )

a*b = a +(a * (b-1))

The above line is unnecessary.

courtrigrad said:
Code:
if( b ==1)
return(a);
else
  return mult(a + a*(b-1)))

Your second return line here is wrong. You're calling mult which should have two arguments...
 
  • #4
courtrigrad said:
Also if you had:

Code:
public void hello(int n )
{
   System.out.println("hello n = ",n);
   if(n > 1)
      hello(n-1);
   System.out.println("goodbye n = ", n);

How would you find the output when a 3 is passed? I got a bumch of hellos and goodbyes, but I got it wrong


Thanks :smile:

I think it should be:
System.out.println("hello n="+n);

Instead of a comma you need a "+".
 

Related to How to Multiply Two Positive Integers Using Recursion | Comp Sci Questions

1. How does recursion work in multiplying two positive integers?

Recursion is a programming technique where a function calls itself repeatedly until a base case is reached. In the case of multiplying two positive integers, the function will call itself with a smaller value of one of the integers until the value of that integer reaches 1, which is the base case.

2. What are the advantages of using recursion in multiplying two positive integers?

One advantage of using recursion is that it can simplify complex problems into smaller, more manageable subproblems. It also allows for more concise and elegant code compared to iterative solutions. Additionally, recursion can be easier to understand and debug for certain problems.

3. Are there any limitations to using recursion in multiplying two positive integers?

One limitation of using recursion is that it can be less efficient compared to iterative solutions, especially for larger inputs. This is because each recursive call requires additional memory and processing time. Additionally, if the base case is not reached, the function will continue to call itself indefinitely, leading to a stack overflow error.

4. How do you determine the base case when using recursion to multiply two positive integers?

The base case for multiplying two positive integers using recursion is when one of the integers reaches a value of 1. This means that the function will stop calling itself and return the value of the other integer, as any number multiplied by 1 will result in the same number.

5. Can negative integers be multiplied using recursion?

Yes, negative integers can also be multiplied using recursion. The same principle applies, where the function will call itself with a smaller value of one of the integers until it reaches -1, which is the base case. However, the multiplication of negative integers may require additional logic to handle the signs of the final result.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
18
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
Replies
9
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
11
Views
917
  • Programming and Computer Science
Replies
2
Views
704
  • Programming and Computer Science
Replies
1
Views
779
  • Programming and Computer Science
2
Replies
35
Views
2K
  • Programming and Computer Science
Replies
17
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
922
Back
Top