Classic number programs
These programs come up again and again in classes, tests and interviews. Each is a small method built from loops and conditions; the output is shown under each example.
Even or odd, and leap years
A leap year divides by 4, except century years, which must also divide by 400:
public class Main {
static boolean isLeap(int year) {
return (year % 4 == 0 && year % 100 != 0) || year % 400 == 0;
}
public static void main(String[] args) {
int[] numbers = {7, 12, 0};
for (int n : numbers) {
System.out.println(n + (n % 2 == 0 ? " is even" : " is odd"));
}
int[] years = {2024, 2026, 1900, 2000};
for (int y : years) {
System.out.println(y + (isLeap(y) ? " is a leap year" : " is not a leap year"));
}
}
}7 is odd
12 is even
0 is even
2024 is a leap year
2026 is not a leap year
1900 is not a leap year
2000 is a leap yearPrime numbers
A prime has exactly two divisors, 1 and itself. Test divisors only up to the square root (d * d <= n): a bigger factor would pair with a smaller one already tested.
public class Main {
static boolean isPrime(int n) {
if (n < 2) {
return false;
}
for (int d = 2; d * d <= n; d++) {
if (n % d == 0) {
return false;
}
}
return true;
}
public static void main(String[] args) {
System.out.println(isPrime(29) + " " + isPrime(91));
int count = 0;
for (int n = 2; n <= 50; n++) {
if (isPrime(n)) {
System.out.print(n + " ");
count++;
}
}
System.out.println();
System.out.println(count + " primes up to 50");
}
}true false
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47
15 primes up to 50The Fibonacci series
public class Main {
public static void main(String[] args) {
int a = 0, b = 1;
for (int i = 1; i <= 10; i++) {
System.out.print(a + " ");
int next = a + b;
a = b;
b = next;
}
System.out.println();
}
}0 1 1 2 3 5 8 13 21 34 Palindromes
Reverse a number's digits and compare; for text, StringBuilder can reverse a string:
public class Main {
static boolean isPalindrome(int n) {
int original = n, reversed = 0;
while (n > 0) {
reversed = reversed * 10 + n % 10;
n /= 10;
}
return reversed == original;
}
public static void main(String[] args) {
System.out.println(121 + " " + isPalindrome(121));
System.out.println(123 + " " + isPalindrome(123));
String word = "level";
String back = new StringBuilder(word).reverse().toString();
System.out.println(word + " " + word.equals(back));
}
}121 true
123 false
level trueCompare strings with .equals(), never ==, which checks whether they are the same object.
Armstrong numbers
The sum of the digits, each raised to the number of digits, equals the number itself (153 = 1 + 125 + 27):
public class Main {
static boolean isArmstrong(int n) {
int digits = String.valueOf(n).length();
int sum = 0, rest = n;
while (rest > 0) {
int d = rest % 10, power = 1;
for (int k = 0; k < digits; k++) {
power *= d;
}
sum += power;
rest /= 10;
}
return sum == n;
}
public static void main(String[] args) {
for (int n = 1; n <= 1000; n++) {
if (isArmstrong(n)) {
System.out.print(n + " ");
}
}
System.out.println();
}
}1 2 3 4 5 6 7 8 9 153 370 371 407 GCD and LCM
Euclid's method: replace (a, b) with (b, a % b) until the remainder is 0. Then LCM = a x b / GCD:
public class Main {
static int gcd(int a, int b) {
while (b != 0) {
int r = a % b;
a = b;
b = r;
}
return a;
}
public static void main(String[] args) {
int x = 48, y = 36;
System.out.println("GCD of " + x + " and " + y + " is " + gcd(x, y));
System.out.println("LCM of " + x + " and " + y + " is " + x * y / gcd(x, y));
}
}GCD of 48 and 36 is 12
LCM of 48 and 36 is 144Swapping two values
public class Main {
public static void main(String[] args) {
int a = 5, b = 9;
int temp = a;
a = b;
b = temp;
System.out.println("a = " + a + ", b = " + b);
}
}a = 9, b = 5