Reverse a string
Write a java program to reverse a given string
import java.util.Scanner;
public class ReverseString {
public static void main(String[] args) {
// Create a Scanner object for input
Scanner scanner = new Scanner(System.in);
// Ask the user to enter a string
System.out.println("Enter a string to reverse:");
String input = scanner.nextLine();
// Reverse the string
String reversed = reverseString(input);
// Print the reversed string
System.out.println("Reversed string: " + reversed);
// Close the scanner
scanner.close();
}
// Method to reverse a string
public static String reverseString(String str) {
StringBuilder sb = new StringBuilder(str);
return sb.reverse().toString();
}
}
If the user inputs:
Enter a string to reverse:
hello
The output will be:
Reversed string: olleh
2. Find the largest element in an array:
Find and print the largest element in an array
import java.util.Scanner;
public class LargestElement {
public static void main(String[] args) {
// Create a Scanner object for input
Scanner scanner = new Scanner(System.in);
// Ask the user for the number of elements in the array
System.out.println("Enter the number of elements in the array:");
int n = scanner.nextInt();
// Create an array to hold the elements
int[] array = new int[n];
// Ask the user to input the elements of the array
System.out.println("Enter the elements of the array:");
for (int i = 0; i < n; i++) {
array[i] = scanner.nextInt();
}
// Find the largest element in the array
int largest = findLargest(array);
// Print the largest element
System.out.println("The largest element in the array is: " + largest);
// Close the scanner
scanner.close();
}
// Method to find the largest element in the array
public static int findLargest(int[] array) {
int max = array[0]; // Assume the first element is the largest
for (int i = 1; i < array.length; i++) {
if (array[i] > max) {
max = array[i]; // Update max if current element is greater
}
}
return max;
}
}
Import the Scanner class:
We import the Scanner class from java.util to take input from the user.
Define the class LargestElement:
The main class is named LargestElement and contains the logic for finding the largest element in an array.
Define the main method:
This is the entry point for the program. It reads input from the user and calls the helper method findLargest to find the largest element.
Create a Scanner object and get array size:
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the number of elements in the array:");
int n = scanner.nextInt();
We create a Scanner object to accept user input. The variable n stores the number of elements in the array.
Create the array and populate it:
int[] array = new int[n];
System.out.println("Enter the elements of the array:");
for (int i = 0; i < n; i++) {
array[i] = scanner.nextInt();
}
int[] array = new int[n];
System.out.println("Enter the elements of the array:");
for (int i = 0; i < n; i++) {
array[i] = scanner.nextInt();
}
int[] array = new int[n];
System.out.println("Enter the elements of the array:");
for (int i = 0; i < n; i++) {
array[i] = scanner.nextInt();
}
We declare an integer array array with n elements. The for loop is used to populate the array by reading each element from the user.
Call the findLargest method:
int largest = findLargest(array);
The findLargest method is called to find the largest element in the array. The result is stored in the variable largest.
Define the findLargest method:
public static int findLargest(int[] array) {
int max = array[0]; // Assume the first element is the largest
for (int i = 1; i < array.length; i++) {
if (array[i] > max) {
max = array[i]; // Update max if current element is greater
}
}
return max;
}
The findLargest method iterates through the array and compares each element with the current largest value (max). If a larger element is found, max is updated.
Print the largest element:
System.out.println("The largest element in the array is: " + largest);
The largest element found is printed to the console.
Close the Scanner:
scanner.close();
The Scanner object is closed to free resources.
Input:
Enter the number of elements in the array:
5
Enter the elements of the array:
10 25 17 9 30
Output:
The largest element in the array is: 30
The program reads an array of integers from the user.
It iterates through the array to find the largest element.
Finally, it prints the largest element.
Let me know if you'd like further clarification or any modifications!
3. Check for palindrome
determine if a given string is a palindrome
(reads the same backward as forward)
4. Factorial Calculation:
Write a function to calculate the factorial of the number
5. Fibonacci series
Generate the first n numbers in the fibonacci sequence
6. Check for Prime Number
Write a program to check if a given number is prime
7. String Anagrams
Determine if two strings are anagrams of each other
8. Arrange Sorting
Implement sorting algorithms like bubble, Sort, Merge Sort or Quick sort.
9. Binary Search
Implement a binary search algorithm to find an element in a sorted array
10. Duplicate element in an Array
Find and print duplicate element in an Array
11. Linked List Reversal
Reverse a singly-linked list
12. Matrix Operations
Perform matrix operations like addition, multiplication or transpose
13. Implement a stake
Create a stack data structure and implement basic opertaions (push and pop)
14. Implement a Queue:
Create a Queue data structure and implement basic operations (enqueue and dequeue)