Java: Armstrong, Palindrome & Prime Numbers



Before beginning to write code, you should know what these numbers are. So, let's start with a little introduction into these numbers.
Armstrong Number
A number is called Armstrong Number, if the sum of its digits to the power of number of digits is number itself. For example,
we have a number, 1634. To determine whether 1634 is an Armstrong, we need to check:
Does 14 + 64 + 34 + 44 equal 1634 ?
Yes! So 1634 is Armstrong Number.
Similarly, 153 is Armstrong because 13 + 53 + 33 equals to 153.
Palindrome Number
A number is said to be a Palindrome, if the reverse of its digit is number itself. For eg. 121, 959, 1441, etc.
Prime Number
A natural number greater than 1 is called a Prime Number, if it has no divisor other than 1 and itself. For eg. 2, 3, 5, 7, ...
The Java program written below has a class named VariousNumbers which contain three public static functions excluding main, listed below:

1. public static boolean Armstrong(int) // Return true, if an integer is found to be an Armstrong, else return false.
2. public static boolean Palindrome(int) // Return true, if an integer is found to be a Palindrome, else return false.
3. public static boolean Prime(int) // Return true, if an integer is found to be a Prime, else return false.
Code

public class VariousNumbers {

    public static void main(String[] args) {
        System.out.println("Armstrong Numbers 1 to 10000 >>");
        for (int i = 1; i <= 10000; i++) {
            if (Armstrong(i) == true) {
                System.out.print(i + " ");
            }
        }

        System.out.println("\nPalindrome Numbers 100 to 300 >>");
        for (int i = 100; i <= 300; i++) {
            if (Palindrome(i) == true) {
                System.out.print(i + " ");
            }
        }

        System.out.println("\nPrime Numbers up to 100 >>");
        for (int i = 1; i <= 100; i++) {
            if (Prime(i) == true) {
                System.out.print(i + " ");
            }
        }
    }

    public static boolean Armstrong(int num) {
        int num1 = num;
        /* Converting Integer to String. It'll help to find number of
        digits in the Integer by using length() */
        String str = Integer.toString(num);
        int rem;
        int result = 0;
        while (num > 0) {
            rem = num % 10;
            num = num / 10;
            result = result + ((int) Math.pow(rem, str.length()));
        }
        if (result == num1) {
            return true;
        } else {
            return false;
        }
    }

    public static boolean Palindrome(int num) {
        int num1 = num;
        int rem;
        int result = 0;
        while (num > 0) {
            rem = num % 10;
            num = num / 10;
            result = (result + rem) * 10;
        }
        result /= 10;
        if (result == num1) {
            return true;
        } else {
            return false;
        }
    }

    public static boolean Prime(int num) {
        if (num < 2) {
            return false;
        }
        int div = num / 2;
        for (int i = 2; i <= div; i++) {
            if (num % i == 0) {
                return false;
            }
        }
        return true;
    }
}

Output

Armstrong Numbers 1 to 10000 >>
1 2 3 4 5 6 7 8 9 153 370 371 407 1634 8208 9474
Palindrome Numbers 100 to 300 >>
101 111 121 131 141 151 161 171 181 191 202 212 222 232 242 252 262 272 282 292
Prime Numbers up to 100 >>
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97