Lucky Number in JavaIn 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 NumberThe 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 ExampleConsider 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. AlgorithmLet's implement the above steps in a Java program. Lucky Number Java ProgramLuckyNumberExample1.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: Next TopicMiddle Digit Number in Java |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India