Skip to content

Commit 79b8eda

Browse files
committed
Recursion
1 parent 5402df6 commit 79b8eda

26 files changed

+940
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*Binary search using recursion*/
2+
3+
package Recursion;
4+
5+
public class binarySearch
6+
{
7+
public static int binarySearch(int a[],int si,int ei,int x)
8+
{
9+
if(si>ei)
10+
{
11+
return -1;
12+
}
13+
14+
int midIndex=(si+ei)/2;
15+
if(a[midIndex]==x)
16+
{
17+
return midIndex;
18+
}
19+
else if(a[midIndex]<x)
20+
{
21+
return binarySearch(a,midIndex+1,ei,x);
22+
}
23+
else
24+
{
25+
return binarySearch(a,si,midIndex-1,x);
26+
}
27+
}
28+
29+
public static void main(String[] args)
30+
{
31+
int[] a={1,3,4,5,6,8};
32+
System.out.println(binarySearch(a,0,a.length-1,6));
33+
}
34+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*Calculate Power
2+
Write a program to find x to the power n (i.e. x^n). Take x and n from the user.*/
3+
4+
package Recursion;
5+
6+
import java.util.Scanner;
7+
8+
public class calculatePower
9+
{
10+
public static int calculatePower(int x, int n)
11+
{
12+
if(n == 0)
13+
{
14+
return 1;
15+
}
16+
int smallOutput = calculatePower(x, n-1);
17+
int output = x*smallOutput ;
18+
return output;
19+
}
20+
21+
public static void main(String[] args)
22+
{
23+
Scanner sc = new Scanner(System.in);
24+
System.out.print("Enter the number : ");
25+
int number = sc.nextInt();
26+
System.out.print("Enter the power : ");
27+
int power = sc.nextInt();
28+
int answer = calculatePower(number, power);
29+
System.out.print(number + "^"+ power +" = "+ answer);
30+
}
31+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*Check AB
2+
Suppose you have a string, S, made up of only 'a's and 'b's. Write a recursive function that checks if the string was generated using the following rules:
3+
a. The string begins with an 'a'
4+
b. Each 'a' is followed by nothing or an 'a' or "bb"
5+
c. Each "bb" is followed by nothing or an 'a'
6+
If all the rules are followed by the given string, return true otherwise return false.*/
7+
8+
package Recursion;
9+
10+
public class checkAB
11+
{
12+
public static boolean checkAb(String s)
13+
{
14+
if (s.length()==0) {
15+
return true;
16+
}
17+
18+
if (s.charAt(0) != 'a'){
19+
return false;
20+
}
21+
22+
if (s.length() >= 3 && "abb".equals(s.substring(0,3))){
23+
return checkAb(s.substring(3));
24+
}
25+
else
26+
{
27+
return checkAb(s.substring(1));
28+
}
29+
}
30+
31+
public static void main(String[] args)
32+
{
33+
System.out.println(checkAb("abababa"));
34+
}
35+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/* Check if the array is sorted
2+
Given an array of length N, you need to find whether the array is sorted or not. */
3+
4+
package Recursion;
5+
6+
import java.util.Scanner;
7+
8+
public class checkArraySorted
9+
{
10+
public static boolean isArraySorted(int arr[],int si)
11+
{
12+
if(si==arr.length-1)
13+
{
14+
return true;
15+
}
16+
if(arr[si]>arr[si+1])
17+
{
18+
return false;
19+
}
20+
boolean isSmallArraySorted=isArraySorted(arr,si+1);
21+
return isSmallArraySorted;
22+
}
23+
24+
// public static boolean isArraySorted(int arr[])
25+
// {
26+
// if(arr.length == 1)
27+
// {
28+
// return true;
29+
// }
30+
// if(arr[0]>arr[1])
31+
// {
32+
// return false;
33+
// }
34+
//
35+
// int smallArray[] = new int[arr.length-1];
36+
// for(int i=1;i<arr.length;i++)
37+
// {
38+
// smallArray[i-1] = arr[i];
39+
// }
40+
// boolean isSmallArraySorted = isArraySorted(smallArray);
41+
// return isSmallArraySorted;
42+
// }
43+
44+
public static void main(String[] arg)
45+
{
46+
Scanner sc = new Scanner(System.in);
47+
System.out.print("Enter the size of array : ");
48+
int size = sc.nextInt();
49+
int[] input_arr = new int[size];
50+
51+
for(int i=0; i<size; i++)
52+
{
53+
System.out.print("Enter the element at "+ i + "th term : ");
54+
input_arr[i] = sc.nextInt();
55+
}
56+
57+
for(int i= 0; i<size; i++)
58+
{
59+
System.out.print(input_arr[i]+ ", ");
60+
}
61+
System.out.println();
62+
63+
//System.out.println(isArraySorted(input_arr));
64+
System.out.println(isArraySorted(input_arr,0));
65+
}
66+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*Check Number in Array
2+
Given an array of length N and an integer x, you need to find if x is present in the array or not. Return true or false.
3+
Do this recursively.*/
4+
5+
package Recursion;
6+
7+
import java.util.Scanner;
8+
9+
public class checkNumberInArray
10+
{
11+
public static boolean checkNumber(int arr[],int x)
12+
{
13+
int n = arr.length;
14+
if (n <= 0)
15+
{
16+
return false;
17+
}
18+
if(arr[0] == x)
19+
{
20+
return true;
21+
}
22+
23+
int smallArray[] = new int[n-1];
24+
for(int i=1;i<n;i++)
25+
{
26+
smallArray[i-1] = arr[i];
27+
}
28+
29+
boolean smallOutput = checkNumber(smallArray,x);
30+
31+
return smallOutput;
32+
}
33+
/*We can optimized the code
34+
public static boolean checkNumber(int arr[], int x, int si)
35+
{
36+
if(arr[si]==x)
37+
{
38+
return true;
39+
}
40+
if(arr[si]!=x)
41+
{
42+
return false;
43+
}
44+
return checkNumber(arr, x , si+1);
45+
}*/
46+
47+
public static void main(String[] arg)
48+
{
49+
Scanner sc = new Scanner(System.in);
50+
System.out.print("Enter the size of array : ");
51+
int size = sc.nextInt();
52+
int[] input_arr = new int[size];
53+
54+
System.out.print("Enter the number for search : ");
55+
int x = sc.nextInt();
56+
57+
for(int i=0; i<size; i++)
58+
{
59+
System.out.print("Enter the element at "+ i + "th term : ");
60+
input_arr[i] = sc.nextInt();
61+
}
62+
63+
for(int i= 0; i<size; i++)
64+
{
65+
System.out.print(input_arr[i]+ ", ");
66+
}
67+
System.out.println();
68+
69+
System.out.println(checkNumber(input_arr, x));
70+
71+
}
72+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*Check Palindrome (recursive)
2+
Check whether a given String S is a palindrome using recursion. Return true or false.*/
3+
4+
package Recursion;
5+
6+
public class checkPalindrome
7+
{
8+
public static boolean Palindrome(String input) {
9+
if(input.length()<=1)
10+
{
11+
return true;
12+
}
13+
else if(input.charAt(0)!=input.charAt(input.length()-1))
14+
{
15+
return false;
16+
}
17+
return Palindrome(input.substring(1, input.length()-1));
18+
19+
}
20+
21+
public static void main(String[] args)
22+
{
23+
System.out.println(Palindrome("racecar"));
24+
}
25+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*Count Zeros
2+
Given an integer N, count and return the number of zeros that are present in the given integer using recursion.*/
3+
4+
package Recursion;
5+
6+
import java.util.Scanner;
7+
8+
public class countZeros
9+
{
10+
public static int count(int n)
11+
{
12+
if(n == 0)
13+
{
14+
return 1;
15+
}
16+
17+
if(n<10)
18+
{
19+
return 0;
20+
}
21+
else if(n%10 == 0)
22+
{
23+
return 1 + count(n/10);
24+
}
25+
return count(n/10);
26+
}
27+
public static void main(String[] arg)
28+
{
29+
Scanner sc = new Scanner(System.in);
30+
System.out.print("Enter the number : ");
31+
int number = sc.nextInt();
32+
33+
System.out.println("Total number of zeros in "+ number +" : "+count(number));
34+
}
35+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*Factorial
2+
Write a program to calculate factorial of N number. Take n from the user.*/
3+
4+
package Recursion;
5+
6+
import java.util.Scanner;
7+
8+
public class factorial
9+
{
10+
public static int fact(int number)
11+
{
12+
if(number == 0)
13+
{
14+
return 1;
15+
}
16+
int smallOutput = fact(number-1);
17+
return number*smallOutput;
18+
}
19+
20+
public static void main(String[] args)
21+
{
22+
System.out.print("Enter the number : ");
23+
Scanner sc = new Scanner(System.in);
24+
int number = sc.nextInt();
25+
26+
int factorialN = fact(number);
27+
System.out.print("factorial of "+ number +" is : "+ factorialN);
28+
}
29+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*FibonacciNumber
2+
Write a program to print nth number in fibonacci series*/
3+
4+
package Recursion;
5+
6+
import java.util.Scanner;
7+
8+
public class fibonacciNumber
9+
{
10+
public static int Fibonacci(int number)
11+
{
12+
if(number == 1 || number == 2)
13+
{
14+
return 1;
15+
}
16+
17+
int f1 = Fibonacci(number-1);
18+
int f2 = Fibonacci(number-2);
19+
int output = f1 + f2;
20+
return output;
21+
}
22+
23+
public static void main(String[] args)
24+
{
25+
Scanner sc = new Scanner(System.in);
26+
System.out.print("Enter the number : ");
27+
int number = sc.nextInt();
28+
System.out.print(Fibonacci(number));
29+
}
30+
}

0 commit comments

Comments
 (0)