Results 1 to 4 of 4
  1. #1
    vicky82 is offline Member
    Join Date
    Dec 2010
    Posts
    2
    Rep Power
    0

    Cool Please Explain me how i got this output.. I am new to java .. so please Explain me...

    public class Exercise {
    static int getAdditionalOdd(int a) {
    if (a <= 1)
    return 1;
    int x = getAdditionalOdd(a -2);
    return a + x ;
    }
    static void showOddNumbers(int a) {
    if (a >= 1) {
    System.out.println(a);
    a -= 2;
    showOddNumbers(a);
    }
    }

    public static void main(String[] args) {
    int number = 9;

    System.out.println("Odd Numbers");
    showOddNumbers(number);

    System.out.println("Sum of Odds: " + getAdditionalOdd(number));
    }
    }

    OUTPUT:
    Odd Numbers:9
    Sum of Odds: 25

    Please Explain me how i got this Answer ... it's confusing for me...

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,252
    Rep Power
    19

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,252
    Rep Power
    19

    Default

    Please Explain me how i got this Answer
    I'll do better than that. I'll point you to a tutorial where you can learn to understand it for yourself.
    The Java™ Tutorials

    Oh, and next time, do your own homework. You don't learn anything by copy/paste.

    db

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,252
    Rep Power
    19

Similar Threads

  1. Replies: 2
    Last Post: 12-13-2010, 01:34 PM
  2. Replies: 8
    Last Post: 06-24-2010, 08:59 AM
  3. [SOLVED] Can anyone explain this pgm's output
    By haoberoi in forum New To Java
    Replies: 13
    Last Post: 11-10-2008, 01:30 PM
  4. Please explain Java
    By MarkWilson in forum New To Java
    Replies: 7
    Last Post: 07-02-2008, 08:38 AM
  5. Iam new in Java Please explain to me
    By vinaytvijayan in forum AWT / Swing
    Replies: 1
    Last Post: 12-30-2007, 11:35 AM

Posting Permissions

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