WAP to input a 4-digit number and check whether it is a unique number or not. A number is said to be unique if all its digits are different or none of its digits are repeated.
Answers
Question:-
WAP to input a 4-digit number and check whether it is a unique number or not.
What is Unique Number?
- A number is said to be unique when all its digits are not same.
- Example:- 1234 is a unique number
Code Language:-
- Not mentioned.
- Since the code Language is not mentioned, it is done in QBASIC, JAVA and PYTHON
________________
Qbasic Code:-
CLS
PRINT "ENTER A NUMBER"
INPUT N
N1=N
N2=N
WHILE N<>0
D=N MOD 10
WHILE N1<>0
K=N1 MOD 10
IF D=K
C=1
N1=N1/10
WEND
N1=N2
N=N/10
WEND
IF C=1
PRINT "NUMBER IS NOT A UNIQUE NUMBER"
ELSE
PRINT "NUMBER IS A UNIQUE NUMBER"
_______________
Java Code:-
package Coder;
import java.util.*;
public class Unique{
public static void main (String ar[]){
Scanner sc=new Scanner (System.in);
System.out.println("Enter a number");
int n=sc.nextInt();
int n1=n,n2=n; //Making copy of two numbers
int d=0,k=0,c=0; //'d' and 'k' are to extract the digits, 'c' is flag variable
while (n!=0){
d=n%10;
while(n1!=0){
k=n1%10;
if(d==k)
c++;
n1/=10;
}
n1=n2;n/=10;
}
if(c==1)
System.out.println("Not a Unique Number");
else
System.out.println("Unique Number");
}
}
•Java output attached
__________________
Python Code:-
n=(int)(input("Enter a number:"))
n1,n2=n,n
c=0
while (n!=0):
d=n%10
while(n1!=0):
k=n1%10
if(d==k):
c+=1
n1//=10
n1=n2
n//=10
if(c==1):
print("Not a Unique Number")
else:
print("Unique Number")
•Python Output Attached
_________________
Variable Description:-
- n:- to accept Number from the user
- n1 and n2:- to make copy of the same number
- c:- Flag variable
- d and k :- to extract the digits of the number