Rohit 202100252
Rohit 202100252
Aim: Write a java program to find factorial using Direct and Tail recursion and check execution time and
performance time complexity using Big O Notation.
Theory: Direct recursion is the type of recursion in which a function directly calls itself within its own block of
code. If a recursive function calling itself and that recursive call is the last statement in the function then it’s
known as Tail Recursion.
Algorithm:
Step 1: Start.
if (n == 0 || n == 1)
return 1
else
return n * findFactorialDirect (n – 1)
if (n == 0 || n == 1)
return result
else
Step 8: End.
Source Code:
int number = 8;
startTime = System.nanoTime();
endTime = System.nanoTime();
ROHIT_202100252
System.out.println("Factorial of " + number + " using direct recursion is: " + factorialDirect);
System.out.println("\nFactorial of " + number + " using tail recursion is: " + factorialTail);
if (n == 0 || n == 1)
return 1;
else
if (n == 0 || n == 1)
return result;
else
Output:
ROHIT_202100252
Aim: Write a java program to determine a factorial of a large number by incorporating BigInteger class
available in MATH API.
Theory: BigInteger class is used for the mathematical operation which involves very big integer calculations
that are outside the limit of all available primitive data types. In this way, BigInteger class is very handy to use
because of its large method library and it is also used a lot in competitive programming.
Algorithm:
Step 1: Start
Step 5: End.
Source Code:
import java.math.BigInteger;
import java.util.Scanner;
int n = scanner.nextInt();
scanner.close();
result = result.multiply(BigInteger.valueOf(i));
return result;
Output:
ROHIT_202100252
Aim: Write a java program to compute Fibonacci numbers using Direct Method & Dynamic Programming.
Theory: The direct method in Java is a method that is invoked directly, without the use of a virtual machine.
This is in contrast to virtual methods, which are invoked indirectly, through the use of a virtual machine.
Dynamic Programming is a method used in mathematics and computer science to solve complex problems by
breaking them down into simpler subproblems. By solving each subproblem only once and storing the
results, it avoids redundant computations, leading to more efficient solutions for a wide range of problems.
Algorithm:
Step 1: Start.
Step 5: End.
Source Code:
import java.util.Scanner;
import java.util.HashMap;
if (n <= 1)
return n;
memo.put(0, 0);
memo.put(1, 1);
if (memo.containsKey(n))
return memo.get(n);
memo.put(n, result);
return result;
ROHIT_202100252
System.out.print("Enter the value of n: ");
int n = scanner.nextInt();
scanner.close();
Output:
ROHIT_202100252
Aim: Write a java program to implement palindrome checking and selection sort recursively.
Theory: Selection sort is a simple and efficient sorting algorithm that works by repeatedly selecting the
smallest (or largest) element from the unsorted portion of the list and moving it to the sorted portion of the
list. The algorithm repeatedly selects the smallest (or largest) element from the unsorted portion of the list and
swaps it with the first element of the unsorted part. This process is repeated for the remaining unsorted
portion until the entire list is sorted.
Algorithm:
Step 1: Start.
Step 5: End.
Source Code:
import java.util.Scanner;
return true;
if (str.charAt(start) != str.charAt(end)) {
return false;
recursiveSelectionSortHelper(arr, 0);
minIndex = i;
ROHIT_202100252
}
arr[start] = arr[minIndex];
arr[minIndex] = temp;
int i = low - 1;
i++;
arr[i] = arr[j];
arr[j] = temp;
arr[i + 1] = arr[high];
arr[high] = temp;
return i + 1;
ROHIT_202100252
System.out.print("Enter a string to check if it's a palindrome: ");
if (isPalindrome(inputString)) {
} else {
selectionArray[i] = scanner.nextInt();
recursiveSelectionSort(selectionArray);
System.out.println();
size = scanner.nextInt();
quickArray[i] = scanner.nextInt();
recursiveQuickSort(quickArray);
System.out.println();
scanner.close();
ROHIT_202100252
}
Output:
deed is a palindrome.
23
46
64
12
87
5 12 23 46 64 87
30
79
21
4 21 30 79
ROHIT_202100252
Aim: Write a java program to compute the greatest common divisor (GCD) using recursion.
Theory: The GCD (Greatest Common Divisor) or HCF (Highest Common Factor) of two numbers is the largest
number that divides both of them.
Algorithm:
Step 1: Start.
Step 6: End.
Source Code:
import java.util.Scanner;
if (num2 == 0) {
return num1;
} else {
scanner.close();
System.out.println("GCD of " + num1 + " and " + num2 + " is: " + findGCD(num1, num2));
Output:
ROHIT_202100252
Aim: Write a java program to implement recursive methods to compute mathematical series.
Source Code:
import java.util.Scanner;
if (number == 0) {
return;
System.out.print(number % 10);
displayDigitsReverse(number / 10);
if (str.isEmpty()) {
return;
System.out.print(str.charAt(str.length() - 1));
if (str.isEmpty()) {
return 0;
if (str.charAt(0) == ch) {
} else {
public static int binarySearch(int[] arr, int target, int low, int high) {
return -1;
if (arr[mid] == target) {
ROHIT_202100252
} else if (arr[mid] < target) {
} else {
if (i == 0) {
return 1;
displayDigitsReverse(num);
System.out.println();
scanner.nextLine();
displayStringReverse(str);
System.out.println();
str = scanner.nextLine();
char ch = scanner.next().charAt(0);
ROHIT_202100252
System.out.print("Enter " + length + " elements in sorted order: ");
arr[i] = scanner.nextInt();
if (index != -1) {
} else {
scanner.close();
Output:
23
34
49
m(1) = 1.5
ROHIT_202100252