Question

Write a recursive method that counts the number of bowling pins given the number of pins in the back row: 1. public static int numPins (int num) numPins(0)-> 0 numPins(1) → 1 numPins(2)-> 3

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Function:

public static int numPins(int num)

    {

        if(num == 0)

            return 0;

        // recursively find the number of pins in num - 1

        else

            return num + numPins(num - 1);

    }

Sample Program:

public class Bowling

{

    public static int numPins(int num)

    {

        if(num == 0)

            return 0;

        // recursively find the number of pins in num - 1

        else

            return num + numPins(num - 1);

    }

   

    public static void main(String[] args)

    {

        int i;

       

        for( i = 0 ; i <= 10 ; i++ )

            System.out.println("No of pins for num = " + i + " : " + numPins(i) + "\n");

    }

}

Sample Output:

C-Useruseresktopjavac Bowling-java C:NUsersNuser Desktop>java Bowling No of pins for num -0 0 No of pins for num1 1 No of pin

Add a comment
Know the answer?
Add Answer to:
Write a recursive method that counts the number of bowling pins given the number of pins...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
Active Questions
ADVERTISEMENT