-1

Just wrote simple java code to print 1 - 100 using recursive method.

Below is the code snippet

public class Test
{
    public static void main(String[] a)
    {
        printNumber(0);
    }

    public static void printNumber(int i)
    {
        if (i < 100)
        {
            System.out.println(i);
            i = i + 1;
            printNumber(i);
        }
        System.out.println(i);
    }
}

but the code prints

0, 1, 2, ............100, 100, 99, ................1

So anyone please tell why it is printing 100,99,98.........1 and what went wrong?

EDIT I tried the logic to print 1 - 100 in all combinations and works well for me but output should be 1 - 99(print from inside condition) and finally 100(print by last print) but output is 1 - 100 and 100 - 1.

so please tell me why 100 to 1 is printing in output.

kindly dont tell me logic because I already got result what i expected

Sasikumar Murugesan
  • 4,412
  • 10
  • 51
  • 74

5 Answers5

2

Use this code it works because when you call printNumber (i) it call and move further. after 100 it will stop calling itself and programcounter return to previous called function thus it printing 100 to 0

   public class Test
{
    public static void main(String[] a)
    {
        printNumber(0);
    }
public static void printNumber(int i)
{
    if (i < 100)
    {
        System.out.println(i);
        i = i + 1;
        printNumber(i);
    }return;
}
}
Divyesh Kalotra
  • 204
  • 1
  • 2
  • 13
1

just remove the second print statement like this:

public static void printNumber(int i)
{
   if(i<=100)
   {
      System.out.println(i);
      i = i + 1;
      printNumber(i);
   }
}
andgalf
  • 170
  • 10
  • 2
    and now explain why your solution doesn't meet the requirements ;) – Stultuske Nov 24 '17 at 11:37
  • I tested this and works for me. why it is printing 100 99 98, by adding the sop? – Sasikumar Murugesan Nov 24 '17 at 11:38
  • 3
    @SasikumarMurugesan this doesn't print 100, so it doesn't work. the print should be before the if, or the condition should be i < 101 – Stultuske Nov 24 '17 at 11:39
  • cos u printed it twice. The recursive call is in the if statement, so when the base case is finally reached at the last number It'll fall out of the if statement for each number it previously printed and use the second print statement, hence printing backward @SasikumarMurugesan – Ayo K Nov 24 '17 at 11:41
  • 1
    yes and to be fully correct, we should call the method with printNumber(1); – andgalf Nov 24 '17 at 11:42
  • That's correct but maybe adding some explanation about how recursion call stack works would be better – YouneL Nov 24 '17 at 12:03
1

The recursive works as it is supposed to do

Each time you call printNumber(i); you're going up in the call stack, whenever you stop calling it, in this case when 1 = 100, it'll unstack and finish the code inside printNumber(), in this case, the rest of the code (after the recursive call ) is another print.

Each of those stacked calls have a different value for i since java is pass by value :

When I say pass by value it means whenever caller has invoked the callee the arguments(ie: the data to be passed to the other function) is copied and placed in the formal parameters (callee's local variables for receiving the input). Java makes data communications from one function to other function only in a pass by value environment.

So it calls the prinln with each of those values ( 100..1 ) note that it does not do the first ( 0 ) since it has been incremented to 1 .

Raphaël
  • 173
  • 11
-1
public class Test 
{
   public static void main(String[] a) 
   {
      printNumber(0);
   }

public static void printNumber(int i)
{
    if(i<=100)
    {
        System.out.println(i);
        i+= 1;
        printNumber(i);
    }

} 
Sasikumar Murugesan
  • 4,412
  • 10
  • 51
  • 74
Shishir M
  • 29
  • 1
  • 3
-1

Nothing went wrong. The frist system out prints the accessing value at the method. So it print the value before invoke printNumber() then when the i value reach 101 the 101th printNumber method ends and the thread come back to the method invked before. So the second System out print the value of i of the specific method.

Kris
  • 1
  • 1