Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit e032c3d

Browse files
basic Recursion program in Java
1 parent 50fa106 commit e032c3d

File tree

4 files changed

+70
-0
lines changed

4 files changed

+70
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package RecursionSeries;
2+
3+
public class Print1ToN {
4+
5+
public static void main(String[] args) {
6+
int n = 10;
7+
printRecursive(n);
8+
}
9+
10+
private static void printRecursive(int n) {
11+
if (n == 0)
12+
return;
13+
14+
printRecursive(n - 1);
15+
System.out.println(n); // backtracking
16+
}
17+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package RecursionSeries;
2+
3+
public class PrintNtimes {
4+
5+
public static void main(String[] args) {
6+
printRecursive("nirmal", 10);
7+
}
8+
9+
private static void printRecursive(String name, int count) {
10+
if (count == 0)
11+
return;
12+
13+
count--;
14+
15+
System.out.println(name);
16+
17+
printRecursive(name, count);
18+
}
19+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package RecursionSeries;
2+
3+
public class SumOfNNaturalNumbers {
4+
5+
public static int sumRecursive(int n) {
6+
if (n == 1)
7+
return n;
8+
9+
int sum = n + sumRecursive(n - 1);
10+
11+
return sum;
12+
}
13+
14+
public static void main(String[] args) {
15+
System.out.println(sumRecursive(5));
16+
}
17+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package RecursionSeries;
2+
3+
public class printNto1 {
4+
5+
public static void main(String[] args) {
6+
int n = 10;
7+
printRecursive(n);
8+
}
9+
10+
private static void printRecursive(int n) {
11+
if (n == 0)
12+
return;
13+
14+
System.out.println(n);
15+
printRecursive(n - 1);
16+
}
17+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /