Results 1 to 2 of 2
  1. #1
    ash12 is offline Member
    Join Date
    Aug 2012
    Posts
    1
    Rep Power
    0

    Default basic recursion problem - code needs to be modified

    I need help fixing this! I just keep getting a million errors urgh --> i need to figure out how to catch the exception (that n < r) properly and continue programming because i need to hard code two more values still (also suggestions on how to do that would be great) --> the prompt is within the code

    Java Code:
    /* The formula for computing the number of ways of choosing r different things from a set of n things is the
    following: C(n, r) = n! / (r! * (n-r)!)
    Write a recursive program (name the class that contains the main method Activity2) that executes C(n,
    r) three times and prints the results. Your execution should use the (n, r) pairs as follows: (2, 4), (5, 3), and
    (24, 12). Hard-code these values and calls to the recursive method into your program. (Do not prompt the
    user to enter them.)
    Note that you will need to devise and program a recursive method that calculates the factorial of a value. In
    the event any of the pairs perform a calculation that throws any exception, catch the exception, print a
    suitable informative message and continue processing. */
     
    import java.math.BigInteger;
     
    public class Activity2 {
     
    public static void main(String[] args) {
     
    try
    {
    int n=2; 
    int r=4; 
    System.out.print("C("+n+","+r+"): "); 
    int fn=factorial(n); 
    int fr=factorial(r);
    int fnr=factorial(n-r);  
    int ncr=fn/(fr*fnr); 
    System.out.println(ncr); 
    } 
    catch (Exception e) {}
    } 
    static int factorial(int n){ 
    	if (n==0)
    		return 1;
    	else
    		return(n*factorial(n-1));
    } 
     
    }

  2. #2
    .paul. is offline Member
    Join Date
    Jun 2012
    Posts
    73
    Blog Entries
    1
    Rep Power
    0

    Default Re: basic recursion problem - code needs to be modified

    your first problem... to solve the negative factorial errors:

    Java Code:
    static int factorial(int n){
         if (n<=0){
            return 1;
            }
        else{
            return(n*factorial(n-1));
            }
    }
    the other errors you will encounter are because the int variables you are using are insufficient to hold such large numbers.

    to hard code several input numbers, instead of int variables use int[] arrays:

    Java Code:
    int[] n = {2,5,24};
    int[] r = {4,3,12};
    + a for loop 0 to 2, placing the reused parts of the code within the loop

Similar Threads

  1. Recursion: Sierpinski Triangle Code Help
    By Renee in forum New To Java
    Replies: 2
    Last Post: 04-24-2012, 06:16 PM
  2. Basic Java Calculator Code Problem
    By SerbianSergeant in forum New To Java
    Replies: 7
    Last Post: 07-22-2011, 02:50 PM
  3. Is there a way to clean up this recursion code?
    By SMHouston in forum New To Java
    Replies: 2
    Last Post: 09-30-2009, 03:30 AM
  4. very basic code
    By BlueJ2008 in forum New To Java
    Replies: 1
    Last Post: 10-14-2008, 02:40 PM
  5. help with basic java code
    By elizabeth in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 07:47 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •