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.
Output:
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);
}
}
}
|
Print Numbers Java |
Post a Comment