Computer Science, asked by diya1078, 1 year ago

write a programe to find the number is lucky or not in java plz help​


pshiv9540: What is meaning of lucky number.
diya1078: Eg 145
diya1078: 1!+4!+5!=145
diya1078: This type of noS r lucky numbers

Answers

Answered by alok38508
4

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


diya1078: I need code
diya1078: Program
diya1078: Source code
Answered by pshiv9540
2

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;

}


diya1078: Thanks
Similar questions