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

  • Thread starter Thread starter courtrigrad
  • Start date Start date
  • Tags Tags
    comp sci
Click For Summary

Homework Help Overview

The discussion revolves around implementing a recursive method for multiplying two positive integers using repeated additions, as well as understanding the output of a recursive method that prints messages based on an integer input.

Discussion Character

  • Exploratory, Conceptual clarification, Mathematical reasoning

Approaches and Questions Raised

  • The original poster attempts to define a recursive multiplication method and questions the correctness of their implementation. They also seek clarification on the output of a separate recursive method that prints messages.

Discussion Status

Some participants provide feedback on the original poster's code, questioning certain lines and suggesting corrections. There is an ongoing exploration of the recursive method's structure and output behavior, with no explicit consensus reached.

Contextual Notes

Participants are discussing the constraints of using positive integers and the expected behavior of recursive functions in the context of programming assignments.

courtrigrad
Messages
1,236
Reaction score
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
are these right?
 
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...
 
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 "+".
 

Similar threads

  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 18 ·
Replies
18
Views
3K
  • · Replies 11 ·
Replies
11
Views
2K
  • · Replies 19 ·
Replies
19
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 7 ·
Replies
7
Views
3K