11) Check Vowel or Consonant in Java:
public class VowelConsonant {
public static void main(String[] args) {
char ch = ‘A’;
if (ch == ‘a’ || ch == ‘e’ || ch == ‘i’ || ch == ‘o’ || ch == ‘u’
|| ch == ‘A’ || ch == ‘E’ || ch == ‘I’ || ch == ‘O’ || ch == ‘U’) {
[Link](ch + ” is a vowel.”);
} else {
[Link](ch + ” is a consonant.”);
Output:
A is a vowel.
12) Calculate Compound Interest in Java:
public class CompoundInterest {
public static void main(String[] args) {
double principal = 15000, rate = 5.5, time = 3;
double compoundInterest = principal * ([Link]((1 + rate / 100), time)) –
principal;
[Link](“Compound Interest: ” + compoundInterest);
}
Output:
Compound Interest: 2653.4375
13) Java Program to Calculate Simple Interest:
import [Link];
public class SimpleInterest {
public static void main(String[] args) {
Scanner input = new Scanner([Link]);
[Link](“Enter principal amount: “);
double principal = [Link]();
[Link](“Enter rate of interest: “);
double rate = [Link]();
[Link](“Enter time period in years: “);
double time = [Link]();
double simpleInterest = (principal * rate * time) / 100;
[Link](“Simple Interest: ” + simpleInterest);
[Link]();
Output:
Enter principal amount: 5000
Enter rate of interest: 2.5
Enter time period in years: 3
Simple Interest: 375.0
14) Java Program to Find Quotient and Remainder:
import [Link];
public class QuotientRemainder {
public static void main(String[] args) {
Scanner input = new Scanner([Link]);
[Link](“Enter dividend: “);
int dividend = [Link]();
[Link](“Enter divisor: “);
int divisor = [Link]();
int quotient = dividend / divisor;
int remainder = dividend % divisor;
[Link](“Quotient: ” + quotient);
[Link](“Remainder: ” + remainder);
[Link]();
Output:
Enter dividend: 17
Enter divisor: 5
Quotient: 3
Remainder: 2
15) Java Program to Calculate Power of a Number:
import [Link];
public class PowerOfNumber {
public static void main(String[] args) {
Scanner input = new Scanner([Link]);
[Link](“Enter base: “);
int base = [Link]();
[Link](“Enter exponent: “);
int exponent = [Link]();
long result = 1;
while (exponent != 0) {
result *= base;
–exponent;
[Link](“Result: ” + result);
[Link]();
Output:
Enter base: 3
Enter exponent: 4
Result: 81
16) Java Program to Convert char to String and String to Char:
public class CharStringConversion {
public static void main(String[] args) {
// Convert char to String
char ch = ‘A’;
String str = [Link](ch);
[Link](“Char to String: ” + str);
// Convert String to char
String s = “Hello”;
char c = [Link](0);
[Link](“String to Char: ” + c);
}
17) Java Program to Find Duplicate Characters in a String:
import [Link];
import [Link];
public class DuplicateCharacters {
public static void main(String[] args) {
String str = “programming”;
Map<Character, Integer> charCountMap = new HashMap<>();
for (char ch : [Link]()) {
if ([Link](ch)) {
[Link](ch, [Link](ch) + 1);
} else {
[Link](ch, 1);
}
}
[Link](“Duplicate Characters:”);
for ([Link]<Character, Integer> entry : [Link]()) {
if ([Link]() > 1) {
[Link]([Link]() + ” – ” + [Link]() + ” times”);
}
18) Java Program to Check Palindrome String using Stack, Queue, For, and
While loop:
import [Link];
import [Link];
import [Link];
public class PalindromeCheck {
public static void main(String[] args) {
String str = “racecar”;
Stack<Character> stack = new Stack<>();
Queue<Character> queue = new LinkedList<>();
for (char ch : [Link]()) {
[Link](ch);
[Link](ch);
}
boolean isPalindrome = true;
while (![Link]() && ![Link]()) {
if (![Link]().equals([Link]())) {
isPalindrome = false;
break;
if (isPalindrome) {
[Link](str + ” is a palindrome.”);
} else {
[Link](str + ” is not a palindrome.”);
}
19) Java Program to Sort Strings in Alphabetical Order:
import [Link];
public class SortStrings {
public static void main(String[] args) {
String[] strings = {“orange”, “apple”, “banana”, “grape”};
[Link](strings);
[Link](“Sorted Strings:”);
for (String s : strings) {
[Link](s);
}
}
20) Java Program to Reverse Words in a String:
public class ReverseWords {
public static void main(String[] args) {
String str = “Hello World”;
String[] words = [Link](” “);
StringBuilder reversed = new StringBuilder();
for (int i = [Link] – 1; i >= 0; i–) {
[Link](words[i]).append(” “);
[Link](“Reversed Words: ” + [Link]().trim());
}
21) Java Program to perform bubble sort on Strings:
import [Link];
public class BubbleSortStrings {
public static void main(String[] args) {
String[] arr = {“banana”, “apple”, “orange”, “grapes”, “pineapple”};
bubbleSort(arr);
[Link](“Sorted Array: ” + [Link](arr));
}
public static void bubbleSort(String[] arr) {
int n = [Link];
for (int i = 0; i < n – 1; i++) {
for (int j = 0; j < n – i – 1; j++) {
if (arr[j].compareTo(arr[j + 1]) > 0) {
// swap arr[j] and arr[j+1]
String temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
Output:
Sorted Array: [apple, banana, grapes, orange, pineapple]
22) Java program to find occurrence of a character in a String:
public class CharacterOccurrences {
public static void main(String[] args) {
String str = “hello world”;
char ch = ‘o’;
int count = 0;
for (int i = 0; i < [Link](); i++) {
if ([Link](i) == ch) {
count++;
[Link](“Occurrences of ‘” + ch + “‘ in the string: ” + count);
Output:
Occurrences of ‘o’ in the string: 2
23) Java program to count vowels and consonants in a String:
public class VowelsConsonantsCount {
public static void main(String[] args) {
String str = “hello world”;
int vowels = 0, consonants = 0;
str = [Link]();
for (int i = 0; i < [Link](); i++) {
char ch = [Link](i);
if (ch == ‘a’ || ch == ‘e’ || ch == ‘i’ || ch == ‘o’ || ch == ‘u’) {
vowels++;
} else if (ch >= ‘a’ && ch <= ‘z’) {
consonants++;
}
[Link](“Vowels: ” + vowels);
[Link](“Consonants: ” + consonants);
Output:
Vowels: 3
Consonants: 7
24) Java Program to check two strings are anagram or not:
import [Link];
public class AnagramCheck {
public static void main(String[] args) {
String str1 = “listen”;
String str2 = “silent”;
boolean isAnagram = checkAnagram(str1, str2);
if (isAnagram) {
[Link](str1 + ” and ” + str2 + ” are anagrams.”);
} else {
[Link](str1 + ” and ” + str2 + ” are not anagrams.”);
public static boolean checkAnagram(String str1, String str2) {
if ([Link]() != [Link]()) {
return false;
}
char[] chars1 = [Link]();
char[] chars2 = [Link]();
[Link](chars1);
[Link](chars2);
return [Link](chars1, chars2);
Output:
listen and silent are anagrams.
25) Java Program to divide a string in ‘n’ equal parts:
public class DivideString {
public static void main(String[] args) {
String str = “abcdefghi”;
int n = 3;
int len = [Link]();
int partLength = len / n;
int extraChars = len % n;
int start = 0;
for (int i = 0; i < n; i++) {
int end = start + partLength + (i < extraChars ? 1 : 0);
String part = [Link](start, end);
[Link](“Part ” + (i + 1) + “: ” + part);
start = end;
Output:
Part 1: abc
Part 2: def
Part 3: ghi
26) Java Program to find all subsets of a string:
import [Link];
import [Link];
public class SubsetsOfString {
public static void main(String[] args) {
String str = “abc”;
List<String> subsets = new ArrayList<>();
generateSubsets(str, 0, “”, subsets);
[Link](“All subsets of \”” + str + “\”: ” + subsets);
private static void generateSubsets(String str, int index, String current,
List<String> subsets) {
if (index == [Link]()) {
[Link](current);
return;
}
generateSubsets(str, index + 1, current + [Link](index), subsets);
generateSubsets(str, index + 1, current, subsets);
Output:
All subsets of “abc”: [, c, b, bc, a, ac, ab, abc]
27) Java Program to find longest substring without repeating characters:
import [Link];
import [Link];
public class LongestSubstringWithoutRepeating {
public static void main(String[] args) {
String str = “abcabcbb”;
[Link](“Longest substring without repeating characters: ” +
longestSubstring(str));
public static int longestSubstring(String s) {
Set<Character> set = new HashSet<>();
int left = 0, right = 0, maxLen = 0;
while (right < [Link]()) {
if ()) {
[Link]([Link](right++));
maxLen = [Link](maxLen, [Link]());
} else {
[Link]([Link](left++));
return maxLen;
Output:
Longest substring without repeating characters: 3
28) Java Program to find longest repeating sequence in a string:
public class LongestRepeatingSequence {
public static void main(String[] args) {
String str = “aabcaabdaab”;
[Link](“Longest repeating sequence: ” +
longestRepeatingSequence(str));
public static String longestRepeatingSequence(String str) {
int n = [Link]();
int[][] dp = new int[n + 1][n + 1];
int longest = 0, endIndex = 0;
for (int i = 1; i <= n; i++) {
for (int j = i + 1; j <= n; j++) {
if ([Link](i – 1) == [Link](j – 1) && dp[i – 1][j – 1] < (j – i)) {
dp[i][j] = dp[i – 1][j – 1] + 1;
if (dp[i][j] > longest) {
longest = dp[i][j];
endIndex = i;
} else {
dp[i][j] = 0;
return [Link](endIndex – longest, endIndex);
Output:
Longest repeating sequence: aab
29) Java Program to remove all the white spaces from a string:
public class RemoveWhiteSpaces {
public static void main(String[] args) {
String str = “This is a test string”;
String trimmedStr = removeWhiteSpaces(str);
[Link](“String after removing white spaces: \”” + trimmedStr +
“\””);
}
public static String removeWhiteSpaces(String str) {
return [Link](“\\s”, “”);
Output:
String after removing white spaces: “Thisisateststring”
30) Program to find number of elements in an array:
public class ArrayLength {
public static void main(String[] args) {
int[] array = {1, 2, 3, 4, 5};
int length = [Link];
[Link](“Number of elements in the array: ” + length);
Output:
Number of elements in the array: 5
31) Java Program to Calculate average of numbers using Array:
public class AverageOfArray {
public static void main(String[] args) {
int[] array = {5, 10, 15, 20, 25};
int sum = 0;
for (int num : array) {
sum += num;
}
double average = (double) sum / [Link];
[Link](“Average of numbers in the array: ” + average);
Output:
Average of numbers in the array: 15.0
32) Java Program to Add the elements of an Array:
public class ArraySum {
public static void main(String[] args) {
int[] array = {1, 2, 3, 4, 5};
int sum = 0;
for (int num : array) {
sum += num;
[Link](“Sum of elements in the array: ” + sum);
Output:
Sum of elements in the array: 15
33) Java Program to reverse an array:
import [Link];
public class ReverseArray {
public static void main(String[] args) {
int[] array = {1, 2, 3, 4, 5};
reverseArray(array);
[Link](“Reversed array: ” + [Link](array));
public static void reverseArray(int[] array) {
int start = 0;
int end = [Link] – 1;
while (start < end) {
int temp = array[start];
array[start] = array[end];
array[end] = temp;
start++;
end–;
Output:
Reversed array: [5, 4, 3, 2, 1]
34) Java Program to sort an array in ascending order:
import [Link];
public class SortArray {
public static void main(String[] args) {
int[] array = {5, 3, 9, 1, 7};
[Link](array);
[Link](“Sorted array in ascending order: ” +
[Link](array));
Output:
Sorted array in ascending order: [1, 3, 5, 7, 9]
35) Java Program to convert char Array to String:
public class CharArrayToString {
public static void main(String[] args) {
char[] charArray = {‘h’, ‘e’, ‘l’, ‘l’, ‘o’};
String str = new String(charArray);
[Link](“Converted String: ” + str);
Output:
Converted String: hello