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:
- public static boolean Armstrong(int) // Return true, if an integer is found to be an Armstrong, else return false.
- public static boolean Palindrome(int) // Return true, if an integer is found to be a Palindrome, else return false.
- 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;
-
-
- 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