Prashnikaप्रश्निका
Code
About
‹ 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

  1. Read the input. If it is not an integer, or n <= 0, print Invalid input and stop.
  2. Set unique = true and copy n into a.
  3. While a > 0:
    • take the last digit d = a % 10 and the digits to its left, b = a / 10;
    • while b > 0, compare the last digit of b (b % 10) with d; if they are equal, set unique = false; then drop that digit with b = b / 10;
    • drop the digit d with a = a / 10.
  4. If unique is still true, print that n is 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

VariableData TypePurpose
argsString[]Command line arguments of main; not used
scScannerReads the input typed by the user
nintThe number to be checked
uniquebooleanStays true while no digit has repeated
aintCopy of n from which each digit is taken in turn
dintThe digit being compared, the last digit of a
bintThe 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 number

Sample 2

Enter a number: 1213
1213 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: 1213, so n = 1213, unique = true and a = 1213.

Stepad = a % 10b = a / 10Comparison b % 10 with dAction
1121331211 and 3Different, b = 12
212133122 and 3Different, b = 1
31213311 and 3Different, b = 0, inner loop ends, a = 121
41211122 and 1Different, b = 1
5121111 and 1Same 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

  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."
  3. Recursive technique "Check a Unique number using a method that calls itself."