[Go to site: main page, start]

0% found this document useful (0 votes)
6 views5 pages

Java Recursion Programs

The document provides a collection of beginner-friendly Java recursion programs, including functions for printing numbers, calculating sums, factorials, powers, and checking palindromes. It also includes methods for array operations such as finding maximum elements, checking if sorted, and performing linear and binary searches. Additionally, there are string manipulation functions for reversing strings and counting vowels.

Uploaded by

lazydemonxx
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views5 pages

Java Recursion Programs

The document provides a collection of beginner-friendly Java recursion programs, including functions for printing numbers, calculating sums, factorials, powers, and checking palindromes. It also includes methods for array operations such as finding maximum elements, checking if sorted, and performing linear and binary searches. Additionally, there are string manipulation functions for reversing strings and counting vowels.

Uploaded by

lazydemonxx
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Java Recursion Programs (Beginner Friendly)

1. Print 1 to N

static void print1ToN(int n) {

if (n == 0) return;

print1ToN(n - 1);

[Link](n + " ");

2. Print N to 1

static void printNTo1(int n) {

if (n == 0) return;

[Link](n + " ");

printNTo1(n - 1);

3. Sum of first N numbers

static int sumN(int n) {

if (n == 0) return 0;

return n + sumN(n - 1);

4. Factorial of a number

static int factorial(int n) {

if (n == 1) return 1;
return n * factorial(n - 1);

5. Power of a number (a^b)

static int power(int a, int b) {

if (b == 0) return 1;

return a * power(a, b - 1);

6. Count digits of a number

static int countDigits(int n) {

if (n == 0) return 0;

return 1 + countDigits(n / 10);

7. Reverse a number

static int reverse(int n, int rev) {

if (n == 0) return rev;

return reverse(n / 10, rev * 10 + n % 10);

8. Check Palindrome (Number)

static boolean isPalindrome(int n, int rev, int original) {

if (n == 0) return rev == original;

return isPalindrome(n / 10, rev * 10 + n % 10, original);

}
9. Print Array Elements

static void printArray(int[] arr, int i) {

if (i == [Link]) return;

[Link](arr[i] + " ");

printArray(arr, i + 1);

10. Find Sum of Array

static int arraySum(int[] arr, int i) {

if (i == [Link]) return 0;

return arr[i] + arraySum(arr, i + 1);

11. Find Maximum Element

static int maxElement(int[] arr, int i) {

if (i == [Link] - 1) return arr[i];

return [Link](arr[i], maxElement(arr, i + 1));

12. Check if Array is Sorted

static boolean isSorted(int[] arr, int i) {

if (i == [Link] - 1) return true;

if (arr[i] > arr[i + 1]) return false;

return isSorted(arr, i + 1);

}
13. Linear Search

static int linearSearch(int[] arr, int i, int key) {

if (i == [Link]) return -1;

if (arr[i] == key) return i;

return linearSearch(arr, i + 1, key);

14. Binary Search (Recursive)

static int binarySearch(int[] arr, int low, int high, int key) {

if (low > high) return -1;

int mid = (low + high) / 2;

if (arr[mid] == key) return mid;

else if (key < arr[mid])

return binarySearch(arr, low, mid - 1, key);

else

return binarySearch(arr, mid + 1, high, key);

15. Reverse a String

static String reverseString(String s) {

if ([Link]()) return s;

return reverseString([Link](1)) + [Link](0);

16. Count Vowels in a String


static int countVowels(String s, int i) {

if (i == [Link]()) return 0;

char ch = [Link]([Link](i));

if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u')

return 1 + countVowels(s, i + 1);

return countVowels(s, i + 1);

You might also like