‹ 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
maincreates the objectob. The constructorUnique()setsnto 0 and creates the arraycountof 10 zeroes.mainreads the numberxand callsob.accept(x), which stores it inn.maincallsob.display(). Ifn <= 0it prints "Invalid input". Otherwise it calls:countDigits(): copiesnintot, and whilet > 0takesd = t % 10, doescount[d]++andt = t / 10.isUnique(): loopsifrom 0 to 9 and returns false if anycount[i]is more than 1; otherwise returns true.
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
| Variable | Data Type | Purpose |
|---|---|---|
n | int | Data member: the number entered by the user |
count | int[] | Data member: count[d] stores how many times digit d occurs in n |
x (in accept) | int | Parameter: the number passed to be stored |
t | int | Copy of n that is reduced digit by digit in countDigits |
d | int | The last digit of t in the current pass |
i | int | Loop counter over the digits 0 to 9 in isUnique |
args | String[] | Command line arguments of main (not used) |
sc | Scanner | Reads the input from the keyboard |
ob | Unique | Object of class Unique created in main |
x (in main) | int | The number typed by the user |
Sample input and output
Sample 1
Enter a number: 2048
2048 is a Unique numberSample 2
Enter a number: 1233
1233 is not a Unique numberSample 3
Enter a number: 7
7 is a Unique numberSample 4
Enter a number: 0
Invalid inputDry run
Input: 1233, so n = 1233.
| Pass | t (before) | d | count after | t (after) |
|---|---|---|---|---|
| 1 | 1233 | 3 | count[3] = 1 | 123 |
| 2 | 123 | 3 | count[3] = 2 | 12 |
| 3 | 12 | 2 | count[2] = 1 | 1 |
| 4 | 1 | 1 | count[1] = 1 | 0 |
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
- Range "Accept two numbers m and n. Display all the Unique numbers between m and n and the count of such numbers."
- Repeated digits "Accept a number and display the digits that occur more than once."