- Notifications
You must be signed in to change notification settings - Fork 366
/
Copy pathone_to_N.java
28 lines (28 loc) · 730 Bytes
/
one_to_N.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
importjava.util.*;
classone_to_N//Head Recursion
{
staticvoidrec(intN) //No other variable needed
{
if(N == 0)
return;
rec(N-1);
System.out.print(N + " ");
}
/* //Tail Recursion alternative
static void rec(int N, int i) //No other variable needed
{
if(i == N)
return;
System.out.print(N + " ");
rec(N,i+1);
} //Will be called using rec(N,0);
*/
publicstaticvoidmain(Stringargs[])
{
ScannerI = newScanner(System.in);
System.out.println("Enter the value of N");
intN = I.nextInt();
rec(N);
I.close();
}
}