Computer Science, asked by sushmita1233, 1 year ago

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 ​


amannishad0512p5zxh6: what is unique no.
amannishad0512p5zxh6: tell me all

Answers

Answered by rakeshchennupati143
1

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:

  1. i took number and converted into string variable using predefined method Interger.toString() which take number as argument and returns into string
  2. now i used nested loop to find the duplicate character withing the string
  3. i used a label to break full loop at once, when ever the flag value changed to true.
  4. when i starts at 0 then j value will be j = i+1 = 1 so 1st character will be checked with the 2nd character.
  5. 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
  6. it not true then i the given number is a unique number
  7. 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  :)


amannishad0512p5zxh6: mark
amannishad0512p5zxh6: me
amannishad0512p5zxh6: me then
sushmita1233: where
amannishad0512p5zxh6: on brainly
amannishad0512p5zxh6: brainly
amannishad0512p5zxh6: okk
amannishad0512p5zxh6: no problem ask me any program
amannishad0512p5zxh6: i am java lover and learner
amannishad0512p5zxh6: gd night
Answered by amannishad0512p5zxh6
1

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.


sushmita1233: even i
amannishad0512p5zxh6: I m not genius on brainly
sushmita1233: even me
amannishad0512p5zxh6: yaa
sushmita1233: ok bye
sushmita1233: good night
amannishad0512p5zxh6: send me
amannishad0512p5zxh6: request
sushmita1233: what
amannishad0512p5zxh6: on fb if you like
Similar questions