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

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

  1. #1
    Junior Member
    Join Date
    Dec 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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 7 3 2 1
    Sum of Odds: 25

    Can any one please explain me how i got the output 25.. its confusing for me
    Last edited by helloworld922; December 13th, 2010 at 10:10 AM.


  2. #2
    Junior Member
    Join Date
    Jul 2010
    Posts
    1
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Please Explain me how i got this output.. I am new to java .. so please Explain m

    sorry for my english
    public class Exercise {
     
    	static int getAdditionalOdd(int a) {
    		System.out.println("iam entered "+a);
     
    		if (a <= 1)
    		{
    		System.out.println("before "+(a-2));
    		return 1; 
     
    		}
     
    		int x = getAdditionalOdd(a -2);
    				System.out.println("a is "+a+" x is "+x);
    		System.out.println("after "+(a+x));
    		return a +x ;
    	}
     
    	static void showOddNumbers(int a) {
     
    		if (a >= 1) {
    		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(9));
    	}
    }
    I changed programm as above for understanding purpose.
    then o/p is

    Odd Numbers
    iam entered 9
    iam entered 7
    iam entered 5
    iam entered 3
    iam entered 1
    before -1
    a is 3 x is 1
    after 4
    a is 5 x is 4
    after 9
    a is 7 x is 9
    after 16
    a is 9 x is 16
    after 25
    Sum of Odds: 25
    Now it is to understand bu observing output.
    For first time a is 9.
    now it again goes to call its own method.
    for four times it goes for call its own method.

    for last repetation a is 1.

    for the first time when it executes return statement it returns 1. That means it came back from last repetation.
    for the second time when it executes return statement it returns 4. That means it came back from lastbutone repetation.

    last finally it returns 25
    Last edited by helloworld922; December 13th, 2010 at 10:10 AM.

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

    JavaPF (December 14th, 2010)

Similar Threads

  1. [SOLVED] Very complex project. It's hard to explain.
    By javapenguin in forum What's Wrong With My Code?
    Replies: 55
    Last Post: November 4th, 2010, 11:30 AM
  2. way too hard to explain this in short. please help guys!!
    By humdinger in forum Collections and Generics
    Replies: 4
    Last Post: March 15th, 2010, 05:57 AM
  3. Can anyone explain this code for me
    By gudwindavids in forum Object Oriented Programming
    Replies: 1
    Last Post: December 11th, 2009, 02:29 PM
  4. Help me this code! Someone please explain strings!
    By helpthiscode in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 16th, 2009, 03:13 AM
  5. can anyone explain this?
    By chronoz13 in forum Java Theory & Questions
    Replies: 4
    Last Post: October 12th, 2009, 02:51 AM