How to Print numbers for 1 to 100 without using for Loop

This is very common interview question asked in java interviews .It can be asked in other way also as an example of recursion .

For the solution of this program we will use recursion mechanism in which a method calls itself again and again till the condition satisfies.





package com.javainhouse;


public class JavaInHousePrintNumber
{
  
  public static void main(String[] args)
  {
    printNumber(100);
    
  }



public static void printNumber(int number)
{
  if(number>0)
  {
    
    
    System.out.print(" "+number);
    number--;
    printNumber(number);
  }
  
}

}

     



Output:

Print Numbers Java

Post a Comment