java program to sum values of an Single Dimensional array
public class ArraySum {
public static void main(String[] args) {
int[] numbers = {5, 2, 8, 1, 9};
int sum = 0;
for (int i = 0; i < numbers.length; i++) {
sum += numbers[i];
}
System.out.println("Sum of array elements: " + sum);
}
}
Java program to sort a numeric array and a string array.
import java.util.Arrays;
public class ArraySorting {
public static void main(String[] args) {
int[] numbers = {5, 2, 8, 1, 9};
System.out.println("Original numeric array: " + Arrays.toString(numbers));
Arrays.sort(numbers);
System.out.println("Sorted numeric array: " + Arrays.toString(numbers));
String[] names = {"Alice", "Bob", "Charlie", "David", "Eve"};
System.out.println("Original string array: " + Arrays.toString(names));
Arrays.sort(names);
System.out.println("Sorted string array: " + Arrays.toString(names));
}
}
Java Program to Calculate the average value of array elements through.
public class ArrayAverage {
public static void main(String[] args) {
int[] numbers = { 2, 4, 6, 8, 10 }; // Input array
double average = calculateAverage(numbers);
System.out.println("The average of the array elements is: " + average);
}
// Function to calculate the average of array elements
public static double calculateAverage(int[] arr) {
int sum = 0;
for (int i = 0; i < arr.length; i++) {
sum += arr[i];
}
double average = (double) sum / arr.length;
return average;
}
}
Java Program to Compare Strings and Count string length.
import java.util.Scanner;
public class StringComparison {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the first string: ");
String firstString = scanner.nextLine();
System.out.print("Enter the second string: ");
String secondString = scanner.nextLine();
'l
if (firstString.equals(secondString)) {
System.out.println("The strings are equal.");
} else {
System.out.println("The strings are not equal.");
}
System.out.println("Length of the first string: " + firstString.length());
System.out.println("Length of the second string: " + secondString.length());
}
}
Java Program to Compute all the permutations of the string.
public class StringPermutations {
public static void main(String[] args) {
String input = "abc";
System.out.println("Permutations of the string: " + input);
computePermutations("", input);
}
private static void computePermutations(String prefix, String remaining) {
int length = remaining.length();
if (length == 0) {
System.out.println(prefix);
return;
}
for (int i = 0; i < length; i++)
{
char chosenChar = remaining.charAt(i);
String newPrefix = prefix + chosenChar;
String newRemaining = remaining.substring(0, i) + remaining.substring(i + 1);
computePermutations(newPrefix, newRemaining);
}
}
}
Java Program to count all vowels in a string.
public class VowelCounter {
public static int countVowels(String str) {
int count = 0;
String vowels = "aeiouAEIOU";
for (int i = 0; i < str.length(); i++)
{
char c = str.charAt(i);
if (vowels.indexOf(c) != -1) {
count++;
}
}
return count;
}
public static void main(String[] args) {
String input = "Hello, World!";
int vowelCount = countVowels(input);
System.out.println("Number of vowels in the string:
" + vowelCount);
}
}