Skip to content

Instantly share code, notes, and snippets.

@AnAverageBeing
Created July 29, 2023 13:01
Show Gist options
  • Save AnAverageBeing/9183f01ffe87a7dbfdac7d1ac3b9c161 to your computer and use it in GitHub Desktop.
Save AnAverageBeing/9183f01ffe87a7dbfdac7d1ac3b9c161 to your computer and use it in GitHub Desktop.

1. 2D Array Operations

Write a Java program to perform the following operations on a given 2D array:

  • Find the sum of all elements in the array.
  • Find the maximum element in the array.
  • Find the minimum element in the array.
  • Calculate the average of all elements in the array.
public class ArrayOperations {
    // Method to find the sum of all elements in the array
    public static int findSum(int[][] arr) {
        // Your code here
    }

    // Method to find the maximum element in the array
    public static int findMax(int[][] arr) {
        // Your code here
    }

    // Method to find the minimum element in the array
    public static int findMin(int[][] arr) {
        // Your code here
    }

    // Method to calculate the average of all elements in the array
    public static double findAverage(int[][] arr) {
        // Your code here
    }

    public static void main(String[] args) {
        // Sample input and output
        // Your code here
    }
}

2. Transpose Matrix

Write a Java program to find the transpose of a given square matrix (N x N). The transpose of a matrix is obtained by swapping its rows with columns.

public class TransposeMatrix {
    // Method to find the transpose of the matrix
    public static int[][] transpose(int[][] matrix) {
        // Your code here
    }

    public static void main(String[] args) {
        // Sample input and output
        // Your code here
    }
}

3. Matrix Multiplication

Write a Java program to perform matrix multiplication for two given matrices (A and B) and display the result matrix (C). Matrix multiplication is a binary operation that takes two matrices as inputs and produces another matrix as output. It involves multiplying the elements of rows of the first matrix with the corresponding elements of columns of the second matrix.

public class MatrixMultiplication {
    // Method to perform matrix multiplication
    public static int[][] multiplyMatrices(int[][] matrixA, int[][] matrixB) {
        // Your code here
    }

    public static void main(String[] args) {
        // Sample input and output
        // Your code here
    }
}

4. String Reversal

Write a Java program to reverse a given string without using any built-in functions. String reversal means to reverse the order of characters in the given string.

public class StringReversal {
    // Method to reverse the given string
    public static String reverseString(String input) {
        // Your code here
    }

    public static void main(String[] args) {
        // Sample input and output
        // Your code here
    }
}

5. Palindrome Check

Write a Java program to check if a given string is a palindrome or not using recursion. A palindrome is a word, phrase, number, or other sequences of characters that reads the same forward and backward (ignoring spaces, punctuation, and capitalization).

public class PalindromeCheck {
    // Method to check if the given string is a palindrome
    public static boolean isPalindrome(String str) {
        // Your code here
    }

    public static void main(String[] args) {
        // Sample input and output
        // Your code here
    }
}

6. String Anagrams

Write a Java program to check if two given strings are anagrams or not. Anagrams are two words or phrases that have the same characters but may be in a different order.

public class StringAnagrams {
    // Method to check if two strings are anagrams
    public static boolean areAnagrams(String str1, String str2) {
        // Your code here
    }

    public static void main(String[] args) {
        // Sample input and output
        // Your code here
    }
}

7. String Permutations

Write a Java program to print all permutations of a given string using recursion. Permutations are all possible arrangements of characters in a string.

public class StringPermutations {
    // Method to print all permutations of a given string
    public static void printPermutations(String str) {
        // Your code here
    }

    public static void main(String[] args) {
        // Sample input and output
        // Your code here
    }
}

8. Fibonacci Series

Write a Java program to generate the Fibonacci series up to a given number using recursion. The Fibonacci series is a sequence of numbers in which each number is the sum of the two preceding ones.

public class FibonacciSeries {
    // Method to generate the Fibonacci series up to a given number
    public static void generateFibonacci(int n) {
        // Your code here
    }

    public static void main(String[] args) {
        // Sample input and output
        // Your code here
    }
}

9. Factorial Calculation

Write a Java program to calculate the factorial of a given number using recursion. The factorial of a non-negative integer n is the product of all positive integers less than or equal to n.

public class FactorialCalculation {
    // Method to calculate the factorial of a given number
    public static int calculateFactorial(int n) {
        // Your code here
    }

    public static void main(String[] args) {
        // Sample input and output
        // Your code here
    }
}

10. Power Calculation

Write a Java program to calculate the power of a given number (base and exponent) using recursion. The power of a number is obtained by multiplying the base with itself exponent times.

public class PowerCalculation {
    // Method to calculate the power of a number
    public static int calculatePower(int base, int exponent) {
        // Your code here
    }

    public static void main(String[] args) {
        // Sample input and output
        // Your code here
    }
}

11. Greatest Common Divisor (GCD)

Write a Java program to find the GCD of two numbers using recursion. The GCD is the largest positive integer that divides each of the numbers without leaving a remainder.

public class GCD {
    // Method to find the GCD of two numbers
    public static int findGCD(int num1, int num2) {
        // Your code here
    }

    public static void main(String[] args) {
        // Sample input and output
        // Your code here
    }
}

12. Check if Array is Sorted

Write a Java program to check if a given array of integers is sorted in non-decreasing order using recursion. A non-decreasing order means that the elements in the array are arranged in ascending order or may have equal consecutive elements.

public class ArraySortingCheck {
    // Method to check if the given array is sorted
    public static boolean isSorted

(int[] arr) {
        // Your code here
    }

    public static void main(String[] args) {
        // Sample input and output
        // Your code here
    }
}

13. Sum of Digits

Write a Java program to calculate the sum of digits of a positive integer using recursion. The sum of digits is obtained by adding up all the digits in the given number.

public class SumOfDigits {
    // Method to calculate the sum of digits of a positive integer
    public static int calculateSumOfDigits(int number) {
        // Your code here
    }

    public static void main(String[] args) {
        // Sample input and output
        // Your code here
    }
}

14. Count Digits

Write a Java program to count the number of digits in a positive integer using recursion. Count the total number of digits present in the given number.

public class CountDigits {
    // Method to count the number of digits in a positive integer
    public static int countDigits(int number) {
        // Your code here
    }

    public static void main(String[] args) {
        // Sample input and output
        // Your code here
    }
}

15. Print Pattern - Recursion

Write a Java program to print a specific pattern using recursion:

1
1 2
1 2 3
1 2 3 4

The pattern consists of rows and columns of numbers, where each row contains numbers from 1 to the row number.

public class PatternPrinting {
    // Method to print the specified pattern using recursion
    public static void printPattern(int n) {
        // Your code here
    }

    public static void main(String[] args) {
        // Sample input and output
        // Your code here
    }
}

16. Find Maximum in an Array using Recursion

Write a Java program to find the maximum element in a given array using recursion. Find the largest element present in the array.

public class FindMaximum {
    // Method to find the maximum element in the array using recursion
    public static int findMax(int[] arr, int index) {
        // Your code here
    }

    public static void main(String[] args) {
        // Sample input and output
        // Your code here
    }
}

17. Reverse Array using Recursion

Write a Java program to reverse a given array using recursion. Reverse the order of elements in the array.

public class ReverseArray {
    // Method to reverse the given array using recursion
    public static void reverse(int[] arr, int start, int end) {
        // Your code here
    }

    public static void main(String[] args) {
        // Sample input and output
        // Your code here
    }
}

18. Check Palindrome using Recursion

Write a Java program to check if a given array is a palindrome or not using recursion. An array is a palindrome if it reads the same from the beginning and end.

public class ArrayPalindrome {
    // Method to check if the given array is a palindrome using recursion
    public static boolean isPalindrome(int[] arr, int start, int end) {
        // Your code here
    }

    public static void main(String[] args) {
        // Sample input and output
        // Your code here
    }
}

19. Number of Occurrences in an Array

Write a Java program to count the number of occurrences of a specific element in a given array using recursion. Count how many times a particular element appears in the array.

public class ElementOccurrences {
    // Method to count the number of occurrences of a specific element in the array using recursion
    public static int countOccurrences(int[] arr, int element, int index) {
        // Your code here
    }

    public static void main(String[] args) {
        // Sample input and output
        // Your code here
    }
}

20. Calculate Average using Recursion

Write a Java program to calculate the average of elements in a given array using recursion. The average is obtained by dividing the sum of all elements by the number of elements in the array.

public class CalculateAverage {
    // Method to calculate the average of elements in the array using recursion
    public static double calculateAverage(int[] arr, int index) {
        // Your code here
    }

    public static void main(String[] args) {
        // Sample input and output
        // Your code here
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment