Thursday , 29 June 2017
Home » ISC Important Programs » Java Program to check for Unique Number

Java Program to check for Unique Number

Question:

Write a Program in Java to input a number and check whether it is a Unique Number or not.

Note: A Unique number is a positive integer (without leading zeros) with no duplicate digits. For example 7, 135, 214 are all unique numbers whereas 33, 3121, 300 are not.

Solution:

/**
* The class UniqueNumber inputs a number and checks whether it is a Unique Number or not
* @author : www.javaforschool.com
* @Program Type : BlueJ Program - Java
*/

import java.io.*;
class UniqueNumber
{
public static void main(String args[])throws IOException
    {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        System.out.print("Enter any number : ");
        int n=Integer.parseInt(br.readLine());

        String s=Integer.toString(n); //converting the number into String form
        int l=s.length();
        int flag=0;

        /* loop for checking whether there are repeated digits */
        for(int i=0;i<l-1;i++)
        {
            for(int j=i+1;j<l;j++)
            {
                if(s.charAt(i)==s.charAt(j)) //if any digits match, then we know it is not a Unique Number
                {
                    flag=1;
                    break;
                }
            }
        }

        if(flag==0)
        System.out.println("**** The Number is a Unique Number ****");
        else
        System.out.println("**** The Number is Not a Unique Number ****");
    }
}

Output:

Enter any number : 3121
**** The Number is Not a Unique Number ****
Enter any number : 5243
**** The Number is a Unique Number ****

Check Also

[Question 2] ISC 2017 Computer Practical Paper Solved – Quiz Result

Solution of Program 2 of ISC 2017 Computer Science Paper 2 (Practical) Exam. Java program to input the answers of each participant row-wise and calculate their marks

5 comments

  1. nice program thanks !!!…

  2. Thank you! Very simple and easy to follow!

  3. can we convert integer into string and use string concepts in the program…?

     

Leave a Reply

Your email address will not be published. Required fields are marked *