File tree Expand file tree Collapse file tree 4 files changed +70
-0
lines changed
Leetcode/leetcodeTags/Recursion Expand file tree Collapse file tree 4 files changed +70
-0
lines changed Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments