Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Java recursion return problem[SOLVED]

  • 06-03-2012 6:11pm
    #1
    Registered Users, Registered Users 2 Posts: 620 ✭✭✭


    Hey Guys I am trying to implement a recursive method that computes the factorial of a number. The code i have works fine however it returns nothing. I know the code works as i have printed the value but it prints through each calculation.any help would be great. The first code is my main class and the second contains the method thats is troubling me
    public class RecursiveMain {
        /**
         * @param args
         */
        public static void main(String[] args) {
    
    
            Iterative it = new Iterative();
            Recursive rec = new Recursive();
            
            
           // it.subtraction(100);
           // rec.myDecrementer(100);
           // it.factorialI(5);
            rec.factorialR(5);
    
        }
    }
    
    public class Recursive {
    	
    	
    	public void myDecrementer(int a){
    		
    		if(a>0){
    			a--;
    			System.out.println(a);
    			myDecrementer(a);
    		if(a==0){
    			
    				System.out.println("The program has finished");
    			}
    			
    		}
    	}
    	
    	public int factorialR(int n){
    		int r;
    		if(n==0)
    			return 0;
    		r=factorialR(n-1)*n;
    		return r;
    	
    		
    		
    		
    }
    	
    	
    	
    	
    	
    	
    
    }
    


Comments

  • Registered Users, Registered Users 2 Posts: 1,311 ✭✭✭Procasinator


    What do you mean returns nothing?
    Is it possible that all you mean is it prints nothing?

    Should you be printing the rec.factorialR(5)?
    System.out.println(rec.factorialR(5));
    


  • Registered Users, Registered Users 2 Posts: 620 ✭✭✭Laika1986


    What do you mean returns nothing?
    Is it possible that all you mean is it prints nothing?

    Should you be printing the rec.factorialR(5)?
    System.out.println(rec.factorialR(5));
    

    Sorry you are correct it prints nothing!ill will try your method and report back!

    I'm a bit of a noob regarding java so this returning blank has thrown me a little I think. What i have done is probably right it's when nothing prints to screen i assumed that it didnt compute properly


  • Registered Users, Registered Users 2 Posts: 806 ✭✭✭Niall09


    What Procasinator said.

    Also, remove
    System.out.println(a);
    

    from your decrementor method. That is why it is printing all the calculations.


  • Registered Users, Registered Users 2 Posts: 620 ✭✭✭Laika1986


    Niall09 wrote: »
    What Procasinator said.

    Also, remove
    System.out.println(a);
    

    from your decrementor method. That is why it is printing all the calculations.

    I need that for part one of the assignment. Should i just create a separate class for the method?


  • Registered Users, Registered Users 2 Posts: 5,246 ✭✭✭conor.hogan.2


    Base case should return 1 not 0 in the Recursive class and the factorialR method.

    Store the answer in a variable in main and print it out. Or System.out.println(rec.factorialR(5)); as others have said.

    You have the decrementor method call commented out so it does not get printed in the example you posted so it is fine.


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 620 ✭✭✭Laika1986


    Base case should return 1 not 0 in the Recursive class and the factorialR method.

    Store the answer in a variable in main and print it out. Or System.out.println(rec.factorialR(5)); as others have said.

    You have the decrementor method call commented out so it does not get printed in the example you posted so it is fine.

    Works like a treat!!Thanks to everyone for their I really appreciate it!


  • Registered Users, Registered Users 2 Posts: 5,246 ✭✭✭conor.hogan.2


    Laika1986 wrote: »
    Works like a treat!!Thanks to everyone for their I really appreciate it!

    I would recommend always using {} for your ifs and elses btw and change the "n==0" to "n <= 1" too.


Advertisement