Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

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: Recursion Trace

Threaded View

  1. #1
    Member
    Join Date
    Jul 2011
    Posts
    31
    My Mood
    Stressed
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Recursion Trace

    I have this recursion that I created the main for but cant figure out the trace as to why it provides the output it does. Can someone provide me a trace?
    import java.util.*;
     
    public class oddRecurrsion{
     
       public static void main(String[] args){
     
          Scanner stdin = new Scanner(System.in);
     
          System.out.print("Enter an integer: ");
          int input = stdin.nextInt();
     
          System.out.println("Integer \"" + input + 
                             "\" equals " + odd(input));
       } //end main
     
          public static int odd(int number) {
     
                if (number == 1) 
                   return 1;
                else
                   return (2 * number - 1) + odd(number - 1);
          } //end odd
     
    } // end class

    OUTPUT:
    Enter an integer: 5
    Integer "5" equals 25

    Enter an integer: 3
    Integer "3" equals 9
    Last edited by helloworld922; August 18th, 2011 at 10:43 PM.


Similar Threads

  1. Recursion Help
    By WeaKeN in forum Algorithms & Recursion
    Replies: 10
    Last Post: May 27th, 2011, 08:17 AM
  2. [SOLVED] Recursion help
    By Actinistia in forum Java Theory & Questions
    Replies: 3
    Last Post: March 21st, 2011, 12:26 PM
  3. Recursion
    By javapenguin in forum Algorithms & Recursion
    Replies: 12
    Last Post: October 18th, 2010, 03:42 PM
  4. Recursion Help
    By vmr in forum Algorithms & Recursion
    Replies: 3
    Last Post: April 1st, 2010, 11:27 PM
  5. Recursion help
    By rhoruns in forum Algorithms & Recursion
    Replies: 4
    Last Post: January 8th, 2010, 11:50 PM