30 useful java programs by vertex

 

30 Useful Java Programs:

1. Reverse String: Problem: Reverse a given string. Solution: Use StringBuilder and its reverse() method.

2. Find Largest Number: Problem: Find the largest number from a given array of integers. Solution: Iterate through the array and compare each element with the current maximum.

3. Check Prime Number: Problem: Determine if a given number is a prime number. Solution: Check if the number is divisible by any number from 2 to the square root of the given number.

4. Calculate Factorial: Problem: Calculate the factorial of a given number. Solution: Use a loop to multiply all numbers from 1 to the given number.

5. Check Palindrome: Problem: Determine if a given string is a palindrome. Solution: Compare the original string with its reversed version.

6. Calculate Fibonacci Sequence: Problem: Generate the first n terms of the Fibonacci sequence. Solution: Use a loop and store the previous two terms to calculate the next term.

7. Find Missing Number: Problem: Find the missing number in a given array of consecutive numbers. Solution: Calculate the sum of all numbers from 1 to the maximum value and subtract the sum of the elements in the array.

8. Sort Array: Problem: Sort a given array of integers in ascending order. Solution: Use built-in sorting methods like Arrays.sort() or implement sorting algorithms like bubble sort or merge sort.

9. Find Duplicate Elements: Problem: Find duplicate elements in a given array. Solution: Use a HashMap to store the count of each element and identify duplicates.

10. Calculate Average: Problem: Calculate the average of a given array of numbers. Solution: Sum all the elements in the array and divide by the total number of elements.

11. Convert String to Integer: Problem: Convert a given string to an integer. Solution: Use Integer.parseInt() method to convert the string.

12. Check Armstrong Number: Problem: Determine if a given number is an Armstrong number. Solution: Calculate the sum of the cubes of each digit of the number and compare it with the original number.

13. Find Greatest Common Divisor (GCD): Problem: Calculate the greatest common divisor of two numbers. Solution: Use the Euclidean algorithm to find the GCD.

14. Find Least Common Multiple (LCM): Problem: Calculate the least common multiple of two numbers. Solution: Use the formula: LCM(a, b) = (a * b) / GCD(a, b)

15. Reverse Array: Problem: Reverse the elements of a given array. Solution: Swap elements from both ends of the array until the middle is reached.

16. Find Substring: Problem: Find a substring within a given string. Solution: Use String.indexOf() method to find the index of the substring.

17. Check Anagram: Problem: Determine if two strings are anagrams of each other. Solution: Sort both strings and compare them.

18. Remove Duplicates from String: Problem: Remove duplicate characters from a given string. Solution: Use a Set to store unique characters and build the string from the set.

19. Count Occurrences of Character: Problem: Count the number of times a specific character appears in a string. Solution: Iterate through the string and increment a counter for each occurrence of the character.

20. Check Balanced Parentheses: Problem: Determine if a string containing parentheses is balanced. Solution: Use a stack to keep track of opening parentheses and match them with closing parentheses.

21. Implement Stack: Problem: Implement a stack data structure using arrays or linked lists. Solution: Implement pushpoppeekisEmpty, and isFull methods for the stack.

22. Implement Queue: Problem: Implement a queue data structure using arrays or linked lists. Solution: Implement enqueuedequeuepeekisEmpty, and isFull methods for the queue.

23. Find Maximum Depth of Binary Tree: Problem: Find the maximum depth of a binary tree. Solution: Use a recursive function to traverse the tree and find the depth of each node.

24. Print Binary Tree Level Order: Problem: Print the nodes of a binary tree in level order. Solution: Use a queue to store the nodes at each level and process them level by level.

25. Find Sum of all Elements in a Binary Tree: Problem: Calculate the sum of all values in a binary tree. Solution: Use a recursive function to traverse the tree and add the value of each node to the sum.

26. Find Maximum Sum Path in Binary Tree: Problem: Find the path with the maximum sum of node values in a binary tree. Solution: Use a recursive function to traverse the tree and calculate the maximum sum path for each subtree.

27. Implement Merge Sort: Problem: Sort an array of elements using the merge sort algorithm. Solution: Divide the array into two halves, sort each half recursively, and merge the sorted halves.

28. Implement Quick Sort: Problem: Sort an array of elements using the quick sort algorithm. Solution: Choose a pivot element, partition the array based on the pivot, and recursively sort the partitions.

29. Find Minimum and Maximum in an Array: Problem: Find the minimum and maximum elements in a given array. Solution: Iterate through the array and compare each element with the current minimum and maximum values.

30. Implement Binary Search: Problem: Search for a specific element in a sorted array using binary search. Solution: Divide the array in half repeatedly until the element is found or the search space is empty.

Popular posts from this blog

pss book : శ్రీకృష్ణుడు దేవుడా, భగవంతుడా completed , second review needed. 26th April 2024

pss book: గురు ప్రార్థనామంజరి . completed 21st july 2024

pss book: కధల జ్ఞానము read review pending. 25th june 2024