Wap to enter a number and check if it is a unique number or not.
Using java and function parameter
Please give the definition of unique number
Urgent
Answers
Unique number:
A number is said to be as unique number only if there are no duplicated digits in that number for example 123 has no duplicated digits so it is a unique number but 1231 has duplicate digits so it is not a unique number
Program:
import java.util.Scanner;
import java.io.*;
public class Main{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter a number : ");
int unique = sc.nextInt();
String st1 = Integer .toString(unique);
boolean flag = false;
int i=0,len = st1.length(),j;
label1:
for( i = 0 ; i < len ; i++){
for( j = i+1 ; j < len ; j++ ){
if( st1 . charAt(i) == st1 . charAt(j) ){
flag = true;
break label1;
}
}
}
if(!flag){
System.out.println(unique+" is a unique number");
}else{
System.out.println(unique+" is not a unique number");
}
}
}
Output-1:
Enter a number : 123
123 is a unique number
Output-2:
Enter a number : 1231
1231 is not a unique number
Explanation:
- i took number and converted into string variable using predefined method Interger.toString() which take number as argument and returns into string
- now i used nested loop to find the duplicate character withing the string
- i used a label to break full loop at once, when ever the flag value changed to true.
- when i starts at 0 then j value will be j = i+1 = 1 so 1st character will be checked with the 2nd character.
- if true then it is a duplicate value so i gave flag = true and at last i check if the flag is not true or not
- it not true then i the given number is a unique number
- if true then it is not a unique number
---Hope you understood my logic behind this program, if you liked it mark as brainliest, in future if you have any doubts according to programming or coding feel free to approach me,i'll explain or in most of the cases solve you doubts or problems :)
import java.util.*;
class Unique
{
boolean unique(int a)
{
int count,i,t=0,h=a;
boolean flag=true;
for(i=0;i<=9;i++)
{
count=0;
while(h!=0)
{
t=h%10;
if(t==i)
count++;
h/=10;
}
h=a;
if(count>1)
{
flag=false;
break;
}
}
return(flag);
}
public static void main( )
{
Scanner sc=new Scanner (System.in);
System.out.println("Enter your number);
int n=sc.nextInt( );
boolean check=ob.unique(n); //call unique function
if(check==true);
System.out.println("Unique Number");
else
System.out.println("Not unique number");
}
}
Sushmitaji ,I have use function to make program.
Hope it will help you.
Mark me brainlest.