‹ All Computer Science notes
ISC Practicals and Theory - Unique Number
Updated 21 Oct 2026
A Unique number is a number in which no digit appears more than once. This program, written in the main method alone, takes each digit from the right using % 10 and / 10, and compares it with all the digits to its left.
Definition
A Unique number is a positive integer in which all the digits are 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.
- $1213$ is not a Unique number: the digit 1 occurs twice.
- $1001$ is not a Unique number: 1 and 0 each occur twice.
- $0$ and negative numbers are not accepted: the program prints "Invalid input".
Algorithm
- Read the input. If it is not an integer, or
n <= 0, printInvalid inputand stop. - Set
unique = trueand copynintoa. - While
a > 0:- take the last digit
d = a % 10and the digits to its left,b = a / 10; - while
b > 0, compare the last digit ofb(b % 10) withd; if they are equal, setunique = false; then drop that digit withb = b / 10; - drop the digit
dwitha = a / 10.
- take the last digit
- If
uniqueis still true, print thatnis a Unique number. Otherwise print that it is not.
Program
Two nested while loops in main compare every digit with the digits to its left, and the boolean unique records whether a repeat was found.
import java.util.*;
/* Program UniqueNumber checks whether a number is a Unique number:
no digit appears more than once in it. */
public class UniqueNumber
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in); // reads input from the keyboard
System.out.print("Enter a number: ");
if (sc.hasNextInt())
{
int n = sc.nextInt(); // the number to be checked
if (n <= 0)
{
System.out.println("Invalid input");
}
else
{
boolean unique = true; // becomes false when a digit repeats
int a = n; // copy of n, from which each digit is taken in turn
while (a > 0)
{
int d = a % 10; // the digit to be compared
int b = a / 10; // the digits to the left of d
while (b > 0)
{
if (b % 10 == d) // the same digit found again: not unique
unique = false;
b = b / 10; // move to the next digit on the left
}
a = a / 10; // drop the digit d
}
if (unique)
System.out.println(n + " is a Unique number");
else
System.out.println(n + " is not a Unique number");
}
}
else
System.out.println("Invalid input");
}
}Variable description table
| Variable | Data Type | Purpose |
|---|---|---|
args | String[] | Command line arguments of main; not used |
sc | Scanner | Reads the input typed by the user |
n | int | The number to be checked |
unique | boolean | Stays true while no digit has repeated |
a | int | Copy of n from which each digit is taken in turn |
d | int | The digit being compared, the last digit of a |
b | int | The digits to the left of d, reduced by 1 digit in each inner pass |
Sample input and output
Sample 1
Enter a number: 2048
2048 is a Unique numberSample 2
Enter a number: 1213
1213 is not a Unique numberSample 3
Enter a number: 7
7 is a Unique numberSample 4
Enter a number: 0
Invalid inputDry run
Input: 1213, so n = 1213, unique = true and a = 1213.
| Step | a | d = a % 10 | b = a / 10 | Comparison b % 10 with d | Action |
|---|---|---|---|---|---|
| 1 | 1213 | 3 | 121 | 1 and 3 | Different, b = 12 |
| 2 | 1213 | 3 | 12 | 2 and 3 | Different, b = 1 |
| 3 | 1213 | 3 | 1 | 1 and 3 | Different, b = 0, inner loop ends, a = 121 |
| 4 | 121 | 1 | 12 | 2 and 1 | Different, b = 1 |
| 5 | 121 | 1 | 1 | 1 and 1 | Same digit, unique = false |
The loops go on, but unique stays false, so the output is 1213 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."
- Recursive technique "Check a Unique number using a method that calls itself."