Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Hi Recursion method in java does not give output for big values.

  1. #1
    Junior Member
    Join Date
    Jul 2011
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Hi Recursion method in java does not give output for big values.

    Hello All,

    I have written following java program using recursion.It is working however it is not giving me ouput for value more than 45..
    I want to find answer for value 50.

    Please help.

    ------------------------

     
    import java.util.Scanner;
     
    public class Test {
     
     
    	public static void main(String[] args) {
     
    		Scanner in =new Scanner(System.in);
    		System.out.println("Enter number");
    		int n = in.nextInt();
     
    		System.out.println(Mymethod(n));
     
    	}
     
     
     
    public static long Mymethod(long n){
     
    	if(n==0){
     
    		return 0;
    	}
     
    	else if(n==1){
     
    		return 1;
    	}
     
       else if(n==2){
     
    		return 1;
    	}
    	else{
     
    	return (Mymethod(n-1))+ (Mymethod(n-2))+ (Mymethod(n-3));
     
    	}
    }
    }
    Last edited by copeg; July 7th, 2011 at 03:39 PM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Hi Recursion method in java does not give output for big values.


  3. The Following User Says Thank You to Norm For This Useful Post:

    copeg (July 7th, 2011)

  4. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Hi Recursion method in java does not give output for big values.

    Define not giving output...does it continue to run? Throw exception? I would presume for a larger value the number of recursive calls would get excruciatingly large and quite some time to process, in other words, you will need to wait for the task to complete.

Similar Threads

  1. The java-output doesn't give the right result?
    By pinotje in forum What's Wrong With My Code?
    Replies: 8
    Last Post: May 31st, 2011, 01:00 PM
  2. how to give inputs to my simple java program
    By pokuri in forum What's Wrong With My Code?
    Replies: 11
    Last Post: January 8th, 2011, 07:36 PM
  3. Store Values in 2D array and output to textarea
    By raverz1tildawn in forum What's Wrong With My Code?
    Replies: 4
    Last Post: January 7th, 2011, 03:13 PM
  4. [SOLVED] Method of Sorting? by assiging numerical values to Strings
    By Mirage in forum Java Theory & Questions
    Replies: 0
    Last Post: June 16th, 2010, 07:49 PM
  5. Replies: 1
    Last Post: April 7th, 2010, 03:44 PM