Write a Java program to find the sum of all prime numbers up to a given number.

Here’s a Java program to find the sum of all prime numbers up to a given number: import java.util.Scanner; public class SumOfPrimes { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print(“Enter a number: “); int num = scanner.nextInt(); int sumOfPrimes = findSumOfPrimes(num); System.out.println(“Sum of prime numbers up to ” + num … Read more

Implement a Java program to rotate an array by a given number of positions.

Here’s a Java program to rotate an array by a given number of positions: import java.util.Arrays; import java.util.Scanner; public class ArrayRotation { public static void main(String[] args) { int[] arr = {1, 2, 3, 4, 5}; Scanner scanner = new Scanner(System.in); System.out.print(“Enter the number of positions to rotate the array: “); int k = scanner.nextInt(); … Read more

Write a Java program to check if a given number is a power of two.

Here’s a Java program to check if a given number is a power of two: import java.util.Scanner; public class PowerOfTwo { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print(“Enter a number: “); int num = scanner.nextInt(); boolean isPowerOfTwo = checkPowerOfTwo(num); if (isPowerOfTwo) { System.out.println(num + ” is a power of two.”); … Read more

Implement a Java program to count the number of vowels and consonants in a string.

Here’s a Java program to count the number of vowels and consonants in a string: import java.util.Scanner; public class VowelConsonant { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print(“Enter a string: “); String str = scanner.nextLine(); int vowelCount = 0, consonantCount = 0; str = str.toLowerCase(); for (int i = 0; … Read more

Write a Java program to check if a given string is a valid palindrome permutation.

Here’s a Java program to check if a given string is a valid palindrome permutation: import java.util.HashMap; import java.util.Map; import java.util.Scanner; public class PalindromePermutation { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print(“Enter a string: “); String str = scanner.nextLine(); boolean isValidPalindromePermutation = checkPalindromePermutation(str); if (isValidPalindromePermutation) { System.out.println(str + ” is … Read more

Implement a Java program to find the intersection of two arrays.

Here’s a Java program to find the intersection of two arrays: import java.util.HashSet; import java.util.Scanner; import java.util.Set; public class Intersection { public static void main(String[] args) { int[] arr1 = new int[5]; int[] arr2 = new int[5]; Scanner scanner = new Scanner(System.in); System.out.println(“Enter 5 numbers for the first array: “); for (int i = 0; … Read more

Implement a Java program to check if a number is Armstrong or not.

Here’s a Java program to check if a number is Armstrong or not: import java.util.Scanner; public class ArmstrongNumber { public static void main(String[] args) { int num, originalNum, remainder, result = 0; Scanner scanner = new Scanner(System.in); System.out.print(“Enter a number: “); num = scanner.nextInt(); originalNum = num; while (originalNum != 0) { remainder = originalNum … Read more

Implement a Java program to find the GCD (Greatest Common Divisor) of two numbers.

Here’s a Java program to find the GCD (Greatest Common Divisor) of two numbers: import java.util.Scanner; public class GCD { public static void main(String[] args) { int num1, num2, gcd; Scanner scanner = new Scanner(System.in); System.out.print(“Enter the first number: “); num1 = scanner.nextInt(); System.out.print(“Enter the second number: “); num2 = scanner.nextInt(); gcd = findGCD(num1, num2); … Read more

Write a Java program to convert a decimal number to binary.

Here’s a Java program to convert a decimal number to binary: import java.util.Scanner; public class DecimalToBinary { public static void main(String[] args) { int decimalNum; Scanner scanner = new Scanner(System.in); System.out.print(“Enter a decimal number: “); decimalNum = scanner.nextInt(); String binaryNum = convertToBinary(decimalNum); System.out.println(“Binary equivalent: ” + binaryNum); } public static String convertToBinary(int decimalNum) { String … Read more