SIGN UP MEMBER LOGIN:    
ARTICLE

Java: Armstrong, Palindrome & Prime Numbers

Posted by Marcus Articles | Java August 03, 2011
Often, such topics are given to students as assignment. This is necessary to clear the concepts or basics of Java programming. This article might help students and newcomers to Java.
Reader Level:


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

Login to add your contents and source code to this article
share this article :
post comment
 
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Nevron Gauge for SharePoint
Become a Sponsor