Prashnikaप्रश्निका
Code
About
‹ All Computer Science notes

ISC Practicals and Theory - Unique Number (OOP)

Updated 20 Oct 2026

A Unique number is a number in which no digit occurs more than once. This version writes the check as a class Unique with data members, a constructor and one-job methods. It counts how many times each digit 0 to 9 occurs in an array, and main only creates an object and calls the methods.

Definition

A Unique number is a positive integer whose digits are all different.

  • $2048$ is a Unique number: the digits 2, 0, 4, 8 are all different.
  • $7$ is a Unique number: a single digit cannot repeat.
  • $1233$ is not a Unique number, because 3 occurs twice.
  • $0$ and negative numbers are not accepted: the program prints "Invalid input".

Algorithm

  1. main creates the object ob. The constructor Unique() sets n to 0 and creates the array count of 10 zeroes.
  2. main reads the number x and calls ob.accept(x), which stores it in n.
  3. main calls ob.display(). If n <= 0 it prints "Invalid input". Otherwise it calls:
    • countDigits(): copies n into t, and while t > 0 takes d = t % 10, does count[d]++ and t = t / 10.
    • isUnique(): loops i from 0 to 9 and returns false if any count[i] is more than 1; otherwise returns true.
  4. display() prints "is a Unique number" or "is not a Unique number".

Program

countDigits() counts, isUnique() decides, display() prints. A copy t is used so that n is kept for printing.

import java.util.*;

/* Class Unique accepts a number and checks whether all its digits are different. */
public class Unique
{
    int n;                // the number entered by the user
    int count[];          // count[d] holds how many times digit d occurs in n

    // Constructor: sets n to 0 and creates the counting array for digits 0 to 9
    Unique()
    {
        n = 0;
        count = new int[10];
    }

    // Stores the number
    // Parameter: x - number entered by the user
    void accept(int x)
    {
        n = x;
    }

    // Counts the occurrences of each digit of n in the array count
    void countDigits()
    {
        int t = n;                 // copy of n, so n itself is not destroyed
        while (t > 0)
        {
            int d = t % 10;        // last digit of t
            count[d]++;
            t = t / 10;            // remove the last digit
        }
    }

    // Checks whether any digit occurs more than once
    // Returns: true if every digit occurs at most once, otherwise false
    boolean isUnique()
    {
        for (int i = 0; i < 10; i++)
        {
            if (count[i] > 1)
                return false;
        }
        return true;
    }

    // Prints the result (a number of zero or less is invalid)
    void display()
    {
        if (n <= 0)
            System.out.println("Invalid input");
        else
        {
            countDigits();
            if (isUnique())
                System.out.println(n + " is a Unique number");
            else
                System.out.println(n + " is not a Unique number");
        }
    }

    // main: creates the object and calls the methods
    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in);   // reads input from the keyboard
        Unique ob = new Unique();              // object of class Unique
        System.out.print("Enter a number: ");
        int x = sc.nextInt();                  // the number typed by the user
        ob.accept(x);
        ob.display();
    }
}

Variable description table

VariableData TypePurpose
nintData member: the number entered by the user
countint[]Data member: count[d] stores how many times digit d occurs in n
x (in accept)intParameter: the number passed to be stored
tintCopy of n that is reduced digit by digit in countDigits
dintThe last digit of t in the current pass
iintLoop counter over the digits 0 to 9 in isUnique
argsString[]Command line arguments of main (not used)
scScannerReads the input from the keyboard
obUniqueObject of class Unique created in main
x (in main)intThe number typed by the user

Sample input and output

Sample 1

Enter a number: 2048
2048 is a Unique number

Sample 2

Enter a number: 1233
1233 is not a Unique number

Sample 3

Enter a number: 7
7 is a Unique number

Sample 4

Enter a number: 0
Invalid input

One value per line

Dry run

Input: 1233, so n = 1233.

Passt (before)dcount aftert (after)
112333count[3] = 1123
21233count[3] = 212
3122count[2] = 11
411count[1] = 10

isUnique() finds count[3] = 2, which is more than 1, so it returns false and the output is 1233 is not a Unique number, the same as the real run above.

Variations

  1. Range "Accept two numbers m and n. Display all the Unique numbers between m and n and the count of such numbers."
  2. Repeated digits "Accept a number and display the digits that occur more than once."