Java: Armstrong, Palindrome & Prime Numbers

Learn Armstrong, palindrome, and prime numbers in Java.

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 an Armstrong number if the sum of its digits to the power of the number of digits is the number itself. For example,
we have a number, 1634. To determine whether 1634 is an Armstrong, we need to check:
 
Does 1^4 + 6^4 + 3^4 + 4^4 equal 1634 ?
 
Yes! So 1634 is Armstrong Number.
 
Similarly, 153 is Armstrong because 1^3 + 5^3 + 3^3 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
  1. public class VariousNumbers {  
  2.  public static void main(String[] args) {  
  3.   System.out.println("Armstrong Numbers 1 to 10000 >>");  
  4.   for (int i = 1; i <= 10000; i++) {  
  5.    if (Armstrong(i) == true) {  
  6.     System.out.print(i + " ");  
  7.    }  
  8.   }  
  9.   System.out.println("\nPalindrome Numbers 100 to 300 >>");  
  10.   for (int i = 100; i <= 300; i++) {  
  11.    if (Palindrome(i) == true) {  
  12.     System.out.print(i + " ");  
  13.    }  
  14.   }  
  15.   System.out.println("\nPrime Numbers up to 100 >>");  
  16.   for (int i = 1; i <= 100; i++) {  
  17.    if (Prime(i) == true) {  
  18.     System.out.print(i + " ");  
  19.    }  
  20.   }  
  21.  }  
  22.  public static boolean Armstrong(int num) {  
  23.   int num1 = num;  
  24.   /* Converting Integer to String. It'll help to find number of 
  25.   digits in the Integer by using length() */  
  26.   String str = Integer.toString(num);  
  27.   int rem;  
  28.   int result = 0;  
  29.   while (num > 0) {  
  30.    rem = num % 10;  
  31.    num = num / 10;  
  32.    result = result + ((int) Math.pow(rem, str.length()));  
  33.   }  
  34.   if (result == num1) {  
  35.    return true;  
  36.   } else {  
  37.    return false;  
  38.   }  
  39.  }  
  40.  public static boolean Palindrome(int num) {  
  41.   int num1 = num;  
  42.   int rem;  
  43.   int result = 0;  
  44.   while (num > 0) {  
  45.    rem = num % 10;  
  46.    num = num / 10;  
  47.    result = (result + rem) * 10;  
  48.   }  
  49.   result /= 10;  
  50.   if (result == num1) {  
  51.    return true;  
  52.   } else {  
  53.    return false;  
  54.   }  
  55.  }  
  56.  public static boolean Prime(int num) {  
  57.   if (num < 2) {  
  58.    return false;  
  59.   }  
  60.   int div = num / 2;  
  61.   for (int i = 2; i <= div; i++) {  
  62.    if (num % i == 0) {  
  63.     return false;  
  64.    }  
  65.   }  
  66.   return true;  
  67.  }  
  68. }  
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