Solved ProgramsICSEClass 9Computer Applications

Special Number Programs in Java for ICSE: Armstrong, Spy, Neon, Disarium, Pronic and More

Every special number asked in ICSE Computer Applications – prime, perfect, Armstrong, palindrome, automorphic, neon, spy, Niven, duck, buzz, Disarium, special and pronic numbers – with definitions, examples and one complete Java program that checks them all.

13 min read
Special Number Programs in Java for ICSE: Armstrong, Spy, Neon, Disarium, Pronic and More

"Check whether a number is a ___ number" is one of the most common question types in ICSE Computer Applications, in Class 9 tests and in the Class 10 board paper. Almost all of them use the same few building blocks. Learn the building blocks once, and you can solve any special number question.

The building blocks

1. Extracting digits

int t = n;
while (t > 0) {
    int d = t % 10;    // last digit
    // use d here
    t = t / 10;        // remove the last digit
}

2. Reversing a number

int rev = 0, t = n;
while (t > 0) {
    rev = rev * 10 + t % 10;
    t /= 10;
}

3. Counting digits

int count = 0;
for (int t = n; t > 0; t /= 10)
    count++;
Always work on a copy (t = n). Most questions compare the result with the original number at the end.

The special numbers

  • Prime: exactly two factors, 1 and itself. *Example: 13*
  • Perfect: the sum of its factors other than itself equals the number. *Example: 28 = 1 + 2 + 4 + 7 + 14*
  • Armstrong: the sum of each digit raised to the power of the number of digits equals the number. *Example: 153 = 1³ + 5³ + 3³*
  • Palindrome: the reverse equals the number. *Example: 121*
  • Automorphic: the square ends with the number itself. *Example: 25² = 625, and 76² = 5776*
  • Neon: the sum of the digits of its square equals the number. *Example: 9² = 81, and 8 + 1 = 9*
  • Spy: the sum of the digits equals the product of the digits. *Example: 1124, since 1 + 1 + 2 + 4 = 8 = 1 × 1 × 2 × 4*
  • Niven (Harshad): divisible by the sum of its digits. *Example: 156, since 156 ÷ 12 = 13*
  • Duck: contains a zero, but not as the first digit. *Example: 1023* (0123 is not a duck number)
  • Buzz: ends with 7 or is divisible by 7. *Examples: 47 and 42*
  • Disarium: the sum of each digit raised to the power of its position equals the number. *Example: 135 = 1¹ + 3² + 5³ = 1 + 9 + 125*
  • Special (Krishnamurthy): the sum of the factorials of its digits equals the number. *Example: 145 = 1! + 4! + 5! = 1 + 24 + 120*
  • Pronic: the product of two consecutive integers. *Example: 12 = 3 × 4*

One program that checks them all

import java.util.Scanner;

class SpecialNumbers {
    static int digitSum(int n) {
        int s = 0;
        for (; n > 0; n /= 10) s += n % 10;
        return s;
    }

    static int countDigits(int n) {
        int c = 0;
        for (; n > 0; n /= 10) c++;
        return c;
    }

    static int factorial(int d) {
        int f = 1;
        for (int i = 2; i <= d; i++) f *= i;
        return f;
    }

    static boolean isPrime(int n) {
        if (n < 2) return false;
        for (int i = 2; i * i <= n; i++)
            if (n % i == 0) return false;
        return true;
    }

    static boolean isPerfect(int n) {
        int s = 0;
        for (int i = 1; i <= n / 2; i++)
            if (n % i == 0) s += i;
        return n > 1 && s == n;
    }

    static boolean isArmstrong(int n) {
        int k = countDigits(n), s = 0;
        for (int t = n; t > 0; t /= 10)
            s += (int) Math.pow(t % 10, k);
        return s == n;
    }

    static boolean isPalindrome(int n) {
        int rev = 0;
        for (int t = n; t > 0; t /= 10) rev = rev * 10 + t % 10;
        return rev == n;
    }

    static boolean isAutomorphic(int n) {
        long sq = (long) n * n;
        int p = (int) Math.pow(10, countDigits(n));
        return sq % p == n;
    }

    static boolean isNeon(int n) {
        return digitSum(n * n) == n;
    }

    static boolean isSpy(int n) {
        int s = 0, p = 1;
        for (int t = n; t > 0; t /= 10) {
            s += t % 10;
            p *= t % 10;
        }
        return s == p;
    }

    static boolean isNiven(int n) {
        return n > 0 && n % digitSum(n) == 0;
    }

    static boolean isDuck(int n) {
        for (int t = n; t > 0; t /= 10)
            if (t % 10 == 0) return true;
        return false;
    }

    static boolean isBuzz(int n) {
        return n % 10 == 7 || n % 7 == 0;
    }

    static boolean isDisarium(int n) {
        int pos = countDigits(n), s = 0;
        for (int t = n; t > 0; t /= 10) {
            s += (int) Math.pow(t % 10, pos);
            pos--;
        }
        return s == n;
    }

    static boolean isSpecial(int n) {
        int s = 0;
        for (int t = n; t > 0; t /= 10) s += factorial(t % 10);
        return s == n;
    }

    static boolean isPronic(int n) {
        for (int i = 0; i * (i + 1) <= n; i++)
            if (i * (i + 1) == n) return true;
        return false;
    }

    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter a positive number: ");
        int n = sc.nextInt();
        System.out.println("Prime: " + isPrime(n));
        System.out.println("Perfect: " + isPerfect(n));
        System.out.println("Armstrong: " + isArmstrong(n));
        System.out.println("Palindrome: " + isPalindrome(n));
        System.out.println("Automorphic: " + isAutomorphic(n));
        System.out.println("Neon: " + isNeon(n));
        System.out.println("Spy: " + isSpy(n));
        System.out.println("Niven: " + isNiven(n));
        System.out.println("Duck: " + isDuck(n));
        System.out.println("Buzz: " + isBuzz(n));
        System.out.println("Disarium: " + isDisarium(n));
        System.out.println("Special: " + isSpecial(n));
        System.out.println("Pronic: " + isPronic(n));
    }
}
Because the number is read as an int, leading zeros disappear. 0123 becomes 123, which correctly makes it not a duck number.

Dry run: is 135 a Disarium number?

digits from the right: 5 (position 3), 3 (position 2), 1 (position 1)
5³ = 125,  3² = 9,  1¹ = 1  →  sum = 135  →  Disarium ✓

Exam tips

  1. In the board exam, write only the check that is asked for, as a complete program with input and output. The all-in-one class above is for practice.
  2. Print the exact messages from the question, such as "SPY NUMBER" or "NOT A SPY NUMBER".
  3. For Armstrong numbers, count the digits first. 1634 is a 4-digit Armstrong number (1⁴ + 6⁴ + 3⁴ + 4⁴ = 1634), so using the cube for every number is wrong.

Practise more in our ICSE Class 9 solved programs and test yourself with the ICSE Class 9 Computer Applications Sample Paper.