-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrintNum1ton.java
More file actions
43 lines (38 loc) · 878 Bytes
/
PrintNum1ton.java
File metadata and controls
43 lines (38 loc) · 878 Bytes
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package Recursion;
public class PrintNum1ton {
static void one_to_n(int n) {
if(n==1) {
System.out.print(n+" ");
return;
}
one_to_n(n-1);
System.out.print(n +" ");
}
static void n_to_one(int n) {
if(n==1) {
System.out.print(n+" ");
return;
}
System.out.print(n+" ");
n_to_one(n-1);
}
static void uninarySub(int n) {
if(n==1) {
System.out.print(n+" ");
return;
}
System.out.print(n+" ");
// uninarySub(n--); -- stack over flow error
uninarySub(--n);
}
public static void main(String[] args) {
one_to_n(5);
System.out.println();
n_to_one(5);
System.out.println();
uninarySub(5);
}
}
//output
// 1 2 3 4 5
// 5 4 3 2 1