Master fundamental Java concepts with these beginner-friendly programs
Count the number of digits in a number without using string conversion.
Write a program to count the number of digits in a number without converting it to a string.
4567
Digits Count: 4
public class CountDigits {
public static void main(String[] args) {
int num = 4567;
int count = 0;
while (num != 0) {
num /= 10;
count++;
}
System.out.println("Digits Count: " + count);
}
}
Repeatedly sum digits of a number until a single digit remains.
Write a program to repeatedly sum the digits of a number until you get a single digit.
9875
Digital Root: 2
public class DigitalRoot {
public static void main(String[] args) {
int num = 9875;
while (num > 9) {
int sum = 0;
while (num != 0) {
sum += num % 10;
num /= 10;
}
num = sum;
}
System.out.println("Digital Root: " + num);
}
}
Check whether a number is divisible by the sum of its digits.
Write a program to check whether a number is a Harshad (Niven) number.
18
Yes, it's a Harshad number.
public class HarshadNumber {
public static void main(String[] args) {
int num = 18;
int sum = 0, temp = num;
while (temp != 0) {
sum += temp % 10;
temp /= 10;
}
if (num % sum == 0) {
System.out.println("Yes, it's a Harshad number.");
} else {
System.out.println("No, it's not a Harshad number.");
}
}
}
Find the Least Common Multiple (LCM) of two numbers using GCD.
Write a program to find the Least Common Multiple (LCM) of two numbers using GCD.
12, 18
LCM: 36
public class LCMUsingGCD {
public static void main(String[] args) {
int a = 12, b = 18;
int gcd = findGCD(a, b);
int lcm = (a * b) / gcd;
System.out.println("LCM: " + lcm);
}
public static int findGCD(int x, int y) {
while (y != 0) {
int temp = y;
y = x % y;
x = temp;
}
return x;
}
}
LCM = (a × b) / GCD.Count the number of trailing zeroes in the factorial of a number.
Write a program to count the number of trailing zeroes in a factorial of a number.
100
Trailing Zeroes: 24
public class TrailingZeroesInFactorial {
public static void main(String[] args) {
int n = 100;
int count = 0;
for (int i = 5; n / i >= 1; i *= 5) {
count += n / i;
}
System.out.println("Trailing Zeroes: " + count);
}
}
Reverse a number without converting it to a string or using arrays.
Write a program to reverse a number without using string conversion or arrays.
1234
Reversed: 4321
public class ReverseNumber {
public static void main(String[] args) {
int num = 1234;
int reversed = 0;
while (num != 0) {
int digit = num % 10;
reversed = reversed * 10 + digit;
num /= 10;
}
System.out.println("Reversed: " + reversed);
}
}
reversed to 0.reversed by multiplying the existing value by 10 and adding the digit.Check if a number is an Armstrong number.
Write a program to check if a number is an Armstrong number.
153
Yes, it's an Armstrong number.
public class ArmstrongNumber {
public static void main(String[] args) {
int num = 153, original = num, result = 0;
int digits = String.valueOf(num).length();
while (num != 0) {
int digit = num % 10;
result += Math.pow(digit, digits);
num /= 10;
}
if (result == original)
System.out.println("Yes, it's an Armstrong number.");
else
System.out.println("No, it's not an Armstrong number.");
}
}
Find the sum of the first N prime numbers.
Write a program to find the sum of the first N prime numbers.
5
Sum: 28
public class SumOfFirstNPrimes {
public static void main(String[] args) {
int n = 5; // Number of prime numbers to sum
int count = 0, num = 2, sum = 0;
while (count < n) {
if (isPrime(num)) {
sum += num;
count++;
}
num++;
}
System.out.println("Sum: " + sum);
}
static boolean isPrime(int number) {
if (number < 2) return false;
for (int i = 2; i <= Math.sqrt(number); i++) {
if (number % i == 0) return false;
}
return true;
}
}
Find the GCD of elements in an array.
Write a program to find the GCD of elements in an array.
[24, 36, 60]
GCD: 12
public class GCDofArray {
public static void main(String[] args) {
int[] arr = {24, 36, 60};
int result = arr[0];
for (int i = 1; i < arr.length; i++) {
result = gcd(result, arr[i]);
}
System.out.println("GCD: " + result);
}
static int gcd(int a, int b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
}
Write a program to count the number of factors of a number.
Write a program to count the number of factors of a number.
12
Total Factors: 6
public class CountTotalFactors {
public static void main(String[] args) {
int num = 12;
int count = 0;
for (int i = 1; i <= num; i++) {
if (num % i == 0) {
count++;
}
}
System.out.println("Total Factors: " + count);
}
}
Write a program to check if a number is a palindrome.
Write a program to check if a number is a palindrome.
1221
Yes, it's a palindrome.
public class PalindromeNumberCheck {
public static void main(String[] args) {
int num = 1221;
int originalNum = num;
int reversed = 0;
while (num != 0) {
int digit = num % 10;
reversed = reversed * 10 + digit;
num /= 10;
}
if (originalNum == reversed) {
System.out.println("Yes, it's a palindrome.");
} else {
System.out.println("Not a palindrome.");
}
}
}
Convert a decimal number to binary without using built-in methods.
Write a program to convert decimal number to binary manually.
10
Binary: 1010
public class DecimalToBinary {
public static void main(String[] args) {
int num = 10;
String binary = "";
while (num > 0) {
int remainder = num % 2;
binary = remainder + binary;
num = num / 2;
}
System.out.println("Binary: " + binary);
}
}
Check if a number can be expressed as the sum of two square numbers.
Check if a number can be expressed as the sum of two square numbers.
50
Yes (e.g., 7² + 1² = 49 + 1 = 50)
public class SumOfTwoSquares {
public static void main(String[] args) {
int n = 50;
boolean found = false;
for (int i = 0; i * i <= n; i++) {
int remainder = n - i * i;
int j = (int)Math.sqrt(remainder);
if (j * j == remainder) {
System.out.println("Yes (e.g., " + i + "² + " + j + "² = " + n + ")");
found = true;
break;
}
}
if (!found) {
System.out.println("No, it cannot be expressed as the sum of two squares.");
}
}
}
Generate all prime numbers up to N using Sieve of Eratosthenes.
Generate all prime numbers up to N using the Sieve of Eratosthenes algorithm.
30
Primes: 2 3 5 7 11 13 17 19 23 29
public class SieveOfEratosthenes {
public static void main(String[] args) {
int n = 30;
boolean[] prime = new boolean[n + 1];
// Initialize all entries as true. A value will be false if it is not a prime.
for (int i = 2; i <= n; i++) {
prime[i] = true;
}
for (int p = 2; p * p <= n; p++) {
if (prime[p]) {
for (int i = p * p; i <= n; i += p) {
prime[i] = false;
}
}
}
System.out.print("Primes: ");
for (int i = 2; i <= n; i++) {
if (prime[i]) {
System.out.print(i + " ");
}
}
}
}
Count digits of a number that divide it completely.
Count digits of a number that divide it completely.
1012
Count: 3
public class DigitsThatDivideNumber {
public static void main(String[] args) {
int num = 1012;
int temp = num;
int count = 0;
while (temp > 0) {
int digit = temp % 10;
temp /= 10;
if (digit != 0 && num % digit == 0) {
count++;
}
}
System.out.println("Count: " + count);
}
}
Write a recursive function to calculate ab.
Write a recursive function to calculate ab.
25
Result: 32
public class PowerUsingRecursion {
public static void main(String[] args) {
int base = 2;
int exponent = 5;
int result = power(base, exponent);
System.out.println("Result: " + result);
}
public static int power(int a, int b) {
if (b == 0) return 1;
return a * power(a, b - 1);
}
}
Write a program to find the largest prime factor of a number.
Write a program to find the largest prime factor of a number.
13195
Largest Prime Factor: 29
public class LargestPrimeFactor {
public static void main(String[] args) {
long num = 13195;
long largestFactor = largestPrimeFactor(num);
System.out.println("Largest Prime Factor: " + largestFactor);
}
public static long largestPrimeFactor(long n) {
long maxPrime = -1;
// Divide out the factor 2
while (n % 2 == 0) {
maxPrime = 2;
n /= 2;
}
// Divide out odd factors
for (long i = 3; i <= Math.sqrt(n); i += 2) {
while (n % i == 0) {
maxPrime = i;
n /= i;
}
}
// If n is a prime number > 2
if (n > 2)
maxPrime = n;
return maxPrime;
}
}
Check whether a number is a perfect square using binary search.
Check whether a number is a perfect square using binary search.
64
Yes, it's a perfect square.
public class PerfectSquareBinarySearch {
public static void main(String[] args) {
int num = 64;
if (isPerfectSquare(num)) {
System.out.println("Yes, it's a perfect square.");
} else {
System.out.println("No, it's not a perfect square.");
}
}
public static boolean isPerfectSquare(int num) {
if (num < 0) return false;
int low = 0, high = num;
while (low <= high) {
int mid = low + (high - low) / 2;
long square = (long) mid * mid;
if (square == num) {
return true;
} else if (square < num) {
low = mid + 1;
} else {
high = mid - 1;
}
}
return false;
}
}
Find the nearest palindrome to a given number.
Find the nearest palindrome to a given number.
123
Nearest Palindrome: 121
public class NearestPalindrome {
public static void main(String[] args) {
int num = 123;
System.out.println("Nearest Palindrome: " + nearestPalindrome(num));
}
public static int nearestPalindrome(int num) {
if (num < 0) return 0; // No negative palindrome considered
int lower = num - 1;
int higher = num + 1;
while (true) {
if (isPalindrome(lower)) return lower;
if (isPalindrome(higher)) return higher;
lower--;
higher++;
}
}
public static boolean isPalindrome(int num) {
int original = num;
int reversed = 0;
while (num > 0) {
int digit = num % 10;
reversed = reversed * 10 + digit;
num /= 10;
}
return original == reversed;
}
}
Count the number of 1s in the binary representation of a number.
Count the number of 1s in the binary representation of a number.
13
Set Bits: 3
public class CountSetBits {
public static void main(String[] args) {
int num = 13;
System.out.println("Set Bits: " + countSetBits(num));
}
public static int countSetBits(int num) {
int count = 0;
while (num > 0) {
count += (num & 1); // Check last bit
num >>= 1; // Right shift
}
return count;
}
}