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 5 of 5

Thread: hey guys new to recursion any ideas for me?

  1. #1
    Junior Member
    Join Date
    Dec 2009
    Posts
    22
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Exclamation hey guys new to recursion any ideas for me?

    Im supposed to Write a recursive method that computes the sum of the first n strictly positive integers. Im new to recursion any help would be great. im getting errors at moment...


    [/
    HTML Code:
    mport java.util.Scanner;
    public class SumTester
    {
    	public static void main(String[] args)
    	{
    		Scanner in = new Scanner(System.in);
    		
    		int sum = 0;		
    		System.out.println("\nPlease Enter n"); // get n from user
    		int n = in.nextInt();	
    	
    	} 
    
    	}
    		// recursively computes the sum of the first n integers
    		public static int sum(int n)
    		{
    			int sum = n;
    			if (N == 1) return 1; 
    			return N * sum(N-1); 
    			System.out.println("Recursively computing sum of the first 5 positive integers");
    			System.out.println("Sum" + n + "=" + sum);
    			//return sum;
    			}
    	
    }


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: hey guys new to recursion any ideas for me?

    Your algorithm needs a little work. Try this:

    1. See if your current number n is 1. If it is, return 1.
    2. Otherwise, sum(n) = sum(n-1) + n

  3. #3
    Junior Member
    Join Date
    Dec 2009
    Posts
    22
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: hey guys new to recursion any ideas for me?

    many thanks for the reply helloworld i have ammended it but having one error cant quiet see where im going wrong.
    HTML Code:
    import java.util.Scanner;
    public class SumTester
    {
    	public static void main(String[] args)
    	{
    		Scanner in = new Scanner(System.in);	
    		System.out.println("\nPlease Enter n"); // get n from user
    		int n = in.nextInt();			
    	}	 
    		
    		// recursively computes the sum of the first n integers
    		public static int sum(int n)
    		{
    			if (n ==1)
    				return 1;
    			else
    				return sum(n) = sum(n-1) + n;
    				
    			System.out.println("sum" + (n) + "=" + (n));
    			}
    	
    }
    Last edited by JavaNoob82; February 23rd, 2010 at 04:52 PM. Reason: wrong program sorry

  4. #4
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: hey guys new to recursion any ideas for me?

    Welcome back, Steve's got you stumped on the first question I see.

    use [code][/code] tags rather than html tags.

    Also you might wish to actually call your recursive method.

  5. #5
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: hey guys new to recursion any ideas for me?

    Your printing out statement in sum can never be reached (can you see why?). Java's compiler recognizes this and will through an error. Put that print out statement somewhere inside your main method, and as chris said, call the method

    System.out.println("The sum is " + sum(n));

Similar Threads

  1. Recursion help
    By rhoruns in forum Algorithms & Recursion
    Replies: 4
    Last Post: January 8th, 2010, 11:50 PM
  2. Simple recursion logic
    By chronoz13 in forum Algorithms & Recursion
    Replies: 3
    Last Post: December 24th, 2009, 10:53 PM
  3. Any ideas for begginer?
    By SwEeTAcTioN in forum Java Theory & Questions
    Replies: 11
    Last Post: October 27th, 2009, 03:28 AM
  4. Problems with recursion
    By KingLane in forum Algorithms & Recursion
    Replies: 4
    Last Post: September 20th, 2009, 11:02 PM
  5. Small Project Ideas
    By Freaky Chris in forum Project Collaboration
    Replies: 20
    Last Post: August 12th, 2009, 12:49 PM