write a programe to find the number is lucky or not in java plz help
Answers
Program to check if a given number is Lucky (all digits are different)
A number is lucky if all digits of the number are different. How to check if a given number is lucky or not.
Examples:
Input: n = 983
Output: true
All digits are different
Input: n = 9838
Output: false
8 appears twice
class GFG
{
// This function returns true if n is lucky
static boolean isLucky(int n)
{
// Create an array of size 10 and initialize all
// elements as false. This array is used to check
// if a digit is already seen or not.
boolean arr[]=new boolean[10];
for (int i = 0; i < 10; i++)
arr[i] = false;
// Traverse through all digits
// of given number
while (n > 0)
{
// Find the last digit
int digit = n % 10;
// If digit is already seen,
// return false
if (arr[digit])
return false;
// Mark this digit as seen
arr[digit] = true;
// Remove the last digit from number
n = n / 10;
}
return true;
}