FIRST PROGRAM :
import java.util.Scanner;
public class Test
{
public static void main(String[]args)
{
Scanner s=new Scanner(System.in);
System.out.println("Put a number");
int a=s.nextInt();
System.out.println("IT'S RESULT");
System.out.println("***************");
for(int i=1;i<=a;i++)
{
System.out.println(i);
}
}
}
SECOND PROGRAM :
print method in PrintNumber class is example of recursive method.
import java.util.Scanner;
public class Test
{
public static void main(String[]args)
{
Scanner s=new Scanner(System.in);
System.out.println("Put a number");
int myNumber=s.nextInt();
PrintNumber pn=new PrintNumber();
System.out.println("IT'S RESULT");
System.out.println("***************");
pn.print(myNumber);
}
}
class PrintNumber
{
public void print(int a)
{
if(a==1)
{
System.out.println(a);
}
else
{
int temp=a-1;
print(temp);
System.out.println(a);
}
}
}
For example if you put 2 into second program, when it enter print method in PrintNumber class, firstly program will check if the number is 1 or not. If not, the number will be minus with 1 before it send it's result to print method again. In this case it will send 1 to print method. So System.out.println(a) for a=2 will stop for a while until a=1. Now after it send 1 to print method, condition for a==1 will be true. 1 will be print, and System.out.println(a) for a=2 will continue again by print 2.
I hope it will help.