1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Comp Sci Questions

  1. Feb 8, 2005 #1
    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 (Text):
    // 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 mult(a + a*(b-1)))


    Also if you had:

    Code (Text):

    public void hello(int n )
       System.out.println("hello n = ",n);
       if(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:
  2. jcsd
  3. Feb 8, 2005 #2
    are these right?
  4. Feb 8, 2005 #3


    User Avatar
    Homework Helper

    The above line is unnecessary.

    Your second return line here is wrong. You're calling mult which should have two arguments...
  5. Feb 8, 2005 #4


    User Avatar
    Homework Helper

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

    Instead of a comma you need a "+".
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?

Similar Discussions: Comp Sci Questions
  1. Comp Sci Problem (Replies: 2)

  2. Comp science hw help (Replies: 6)