-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f72bdc1
commit e721c31
Showing
5 changed files
with
124 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package Java_DSA.Recursion; | ||
import java.util.Scanner; | ||
|
||
public class DisplayNNumbers | ||
{ | ||
public static void printDescending(int n) | ||
{ | ||
if(n==0) | ||
return; | ||
System.out.print(n + " "); | ||
printDescending(n-1); | ||
} | ||
|
||
public static void printAscending(int n) | ||
{ | ||
if(n==0) | ||
return; | ||
printAscending(n-1); | ||
System.out.print(n + " "); | ||
} | ||
|
||
public static void main(String[] args) | ||
{ | ||
Scanner scan = new Scanner(System.in); | ||
int n; | ||
System.out.println("Enter value of n : "); | ||
n = scan.nextInt(); | ||
|
||
printDescending(n); | ||
System.out.println(); | ||
|
||
printAscending(n); | ||
System.out.println(); | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package Java_DSA.Recursion; | ||
import java.util.*; | ||
|
||
public class Factorial | ||
{ | ||
public static int calcFactorial(int n){ | ||
if(n == 1){ | ||
return 1; | ||
} | ||
int fact = calcFactorial(n-1); | ||
int ans = n * fact; | ||
return ans; | ||
} | ||
public static void main(String[] args) { | ||
Scanner sc = new Scanner(System.in); | ||
System.out.println("Enter the value of n : "); | ||
int n = sc.nextInt(); | ||
int result = calcFactorial(n); | ||
System.out.println(result); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package Java_DSA.Recursion; | ||
import java.util.*; | ||
|
||
public class Fibonacci { | ||
public static void fibonacci(int i,int n,int n1,int n2){ | ||
if(i == n){ | ||
return; | ||
} | ||
int n3 = n1 + n2; | ||
System.out.println(n3); | ||
n1 = n2; | ||
n2 = n3; | ||
fibonacci(i+1,n,n1,n2); | ||
|
||
} | ||
public static void main(String[] args) { | ||
Scanner sc = new Scanner(System.in); | ||
System.out.println("Enter the value of n : "); | ||
int n = sc.nextInt(); | ||
int n1 = 0; | ||
int n2 = 1; | ||
System.out.println(0); | ||
System.out.println(1); | ||
fibonacci(2,n,n1,n2); | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package Java_DSA.Recursion; | ||
|
||
public class StringPermutation { | ||
public static void calcPermutation(String str,String perm,int index){ | ||
if(str.length() == 0){ | ||
System.out.println(perm); | ||
return; | ||
} | ||
for(int i = 0;i < str.length();i++){ | ||
char currChar = str.charAt(i); | ||
String newStr = str.substring(0,i) + str.substring(i+1); | ||
calcPermutation(newStr,perm + currChar,index+1); | ||
} | ||
} | ||
public static void main(String[] args) { | ||
String str = "ABC"; | ||
calcPermutation(str,"",0); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package Java_DSA.Recursion; | ||
import java.util.Scanner; | ||
|
||
public class SumOfNNumbers | ||
{ | ||
public static void calculateSum(int i ,int n,int sum){ | ||
if(i == n){ | ||
sum = sum + i; | ||
System.out.println(sum); | ||
return; | ||
} | ||
sum = sum + i; | ||
calculateSum(i+1,n,sum); | ||
} | ||
public static void main(String[] args) { | ||
Scanner sc = new Scanner(System.in); | ||
System.out.println("Enter the value of n : "); | ||
int n = sc.nextInt(); | ||
calculateSum(1,n,0); | ||
} | ||
} |