Javatpoint Logo
Javatpoint Logo

Lucky Number in Java

In this section, we will learn what is a luck number and also create Java programs to check if the given number is a lucky number or not. The lucky number program frequently asked in Java coding tests and academics.

Lucky Number

The sequence of natural numbers or subset of integers that we get after removing second, third, fourth, fifth, and so on number respectively from the sequence. By applying the process there still remains some numbers indefinitely in the sequence such numbers are known as lucky numbers.

Let's understand it through an example.

Lucky Number Example

Consider the sequence of following natural numbers:

1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, ….

Let's remove every second number (2, 4, 6, 8, 10, ……) from the above sequence, we get:

1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, …………

Let's remove every third number (5, 11, 17, 23, ……) from the above sequence, we get:

1, 3, 7, 9, 13, 15, 19, 21, 25, …….

Continue the above process indefinitely by removing the fourth, fifth, sixth,……, and so on, until after a fixed number of steps, certain natural numbers remain indefinitely. The numbers that remains in the sequence are known as lucky Numbers.

Algorithm

Let's implement the above steps in a Java program.

Lucky Number Java Program

LuckyNumberExample1.java

Output 1:

Enter the number: 19
19 is a Lucky Number.

Output 2:

Enter the number: 89
89 is not a Lucky Number.

Let's create another Java program that finds all the lucky numbers for the specified range.

LuckyNumberExample2.java

Output:

Lucky Number in Java




Youtube For Videos Join Our Youtube Channel: Join Now

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA