Java Program to Check Unique Number

In the previous article, we have discussed Java Program to find Reverse Number of a Given Number

In this article we are going to understand what Unique number is and how we can check whether a number is Unique or not in Java with examples.

Program to Check Unique Number

Unique numbers are numbers where the digits are not repeated i.e. number formed without repetition of any digits.

Example:

236: 236 is Unique Number
121: 121 is not Unique Number
54: 54 is Unique number

In the above examples the numbers 236 and 54 are unique numbers. 121 has 1 repeated in it so it it not an unique number.

Interested in programming and want to excel in it by choosing the short ways. Then, practicing with the available Java Program list is mandatory.

  • Unique number in java using for loop
  • Generate unique number in java
  • Unique number list
  • Unique number example
  • Unique number is
  • Java program to check unique number
  • Get char number java

Approach :

  1. We ask the user to enter a number which we store in two integer variables.
  2. We then use a while loop to iterate through all the digits in the number and check if they occur again.
  3. If the occurrence of any number is more than 1 it is not an unique number, else it is.

Let’s see different ways to check unique number.

Method-1: Java Program to Check Unique Number By Using Static Value

import java.util.Scanner;
public class UniqueNumber{
    public static void main(String args[])
    {
        //A number declared
        int num = 78;

        //n1 and n2 are variable holders for num, counter is the digit counter
        int n1 = num, n2 = num, remainder1, remainder2, counter=0;
        //Checking the occurence of digits
        while (n1 > 0)
        {
            remainder1 = n1 % 10;
            while (n2 > 0)
            {
                remainder2 = n2 % 10;
                if (remainder1 == remainder2)
                {
                    counter++;
                }
                n2 = n2 / 10;
            }
            n1 = n1 / 10;
        }

        if(counter==1)
        {
            System.out.println(num+" is an unique number");
        }
        else
        {
            System.out.println(num+" is not an unique number");
        }

    }
}
Output:

78 is an unique number

Java Program to Check Unique Number By User Input Value

import java.util.Scanner;
public class UniqueNumber{
    public static void main(String args[])
    {
        //Taking the number as input from the user using scanner class
        Scanner scan = new Scanner(System.in);
        System.out.print("Enter a number : ");
        int num = scan.nextInt();

        //n1 and n2 are variable holders for num, counter is the digit counter
        int n1 = num, n2 = num, remainder1, remainder2, counter=0;
        //Checking the occurence of digits
        while (n1 > 0)
        {
            remainder1 = n1 % 10;
            while (n2 > 0)
            {
                remainder2 = n2 % 10;
                if (remainder1 == remainder2)
                {
                    counter++;
                }
                n2 = n2 / 10;
            }
            n1 = n1 / 10;
        }

        if(counter==1)
        {
            System.out.println(num+" is an unique number");
        }
        else
        {
            System.out.println(num+" is not an unique number");
        }

    }
}
Output:

Case-1

Enter a number : 56
56 is an unique number

Case-2

Enter a number : 111
111 is not an unique number

Method-3: Java Program to Check Unique Number By Using User Defined Method

import java.util.Scanner;
public class UniqueNumber{
    public static void main(String args[])
    {
        //Taking the number as input from the user using scanner class
        Scanner scan = new Scanner(System.in);
        System.out.print("Enter a number : ");
        int num = scan.nextInt();
        //calling the user defined method
        //to check unique number or not.
        checkNumber(num);
    }

     //checkNumber() method to check unique number
    public static void checkNumber(int num)
    {

        //n1 and n2 are variable holders for num, counter is the digit counter
        int n1 = num, n2 = num, remainder1, remainder2, counter=0;
        //Checking the occurence of digits
        while (n1 > 0)
        {
            remainder1 = n1 % 10;
            while (n2 > 0)
            {
                remainder2 = n2 % 10;
                if (remainder1 == remainder2)
                {
                    counter++;
                }
                n2 = n2 / 10;
            }
            n1 = n1 / 10;
        }

        if(counter==1)
        {
            System.out.println(num+" is an unique number");
        }
        else
        {
            System.out.println(num+" is not an unique number");
        }

    }
}
Output: 

Case-1 

Enter a number : 56 
56 is an unique number 

Case-2 

Enter a number : 111 
111 is not an unique number

Have you mastered basic programming topics of java and looking forward to mastering advanced topics in a java programming language? Go with these ultimate Advanced java programs examples with output & achieve your goal in improving java coding skills.

Analyse yourself:

  1. How to check number or character in java
  2. How to get each digit of number in java
  3. Find the number java program
  4. How to get digits of number in java
  5. How to get unique number in java
  6. Find unique digits in a number java

Related Java Programs: