Java Program to Check Lucky Number

In the previous article, we have seen Java Program to Check Katadrome Number

In this article we are going to understand what Lucky number is and how we can check whether a number is Lucky or not in Java with examples.

Java Program to Check Lucky Number

Lucky numbers are the sequence of natural numbers that we get after removing every second, third, fourth, fifth, sixth and so on numbers from the sequence. Means after removing some elements by this approach, still at last remains some numbers, which are called lucky number.

Example:

Number Sequence:
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, ….
After removing every 2nd element, now the number sequence is:
1, 3, 5, 7, 9, 11, 13, 15, 17, 19 ….
After removing every 3rd element, now the number sequence is:
1, 3, 7, 9, 13, 15, 19, …
After continuing this step with every 4th, 5th, 6th and so on at some point definitely we will get some 
sequence of numbers left, which resulting sequence numbers are the lucky numbers.

Let’s see different ways to check lucky number.

Approach:

  1. Declare/Enter a number which we store in an integer variable num.
  2. Initialize count value as 2.
  3. Call the user defined method recursively to check if it is lucky number or not.
  4. Inside method, Check the number if it is less than 2 then it is lucky number.
  5. If it is divisible by count  then it is not a lucky number.
  6. Then get the position of number by num=num-(num/count)
  7. Increment count value.
  8. As recursively calling this method, so repeat steps within the method.

Method-1: Java Program to Check Lucky Number By Using Static Value

import java.util.*;  
public class Main 
{  
    public static int count = 2;  
    
    //checkNumber() user defined method 
    //To check if it is a lucky  number or not
    static boolean checkNumber(int num)  
    {  
        //means if number less than 2 
        //then considered as lucky number
        //Hence return true
        if(count > num)  
            return true;
            
        //Divide num with count value
        //If the number is divisible
        //then return false
        if(num%count == 0)  
            return false; 
            
        //here we are getting the position 
        //of the given number  
        num = num-(num/count);  
        
        //incrementing the count value by 1
        count++;  
        return checkNumber(num);  
    } 
    
    //Main method
    public static void main (String args[])  
    {  
        //A number declared
        int num=19;
        //Within the if condition checkNumber() method called
        //if the method returns value as true then it is lucky number.
        if(checkNumber(num))  
            System.out.println(num+" is a Lucky Number."); 
        //Else it is not a lucky number
        else  
            System.out.println(num+" is not a Lucky Number.");  
    }  
}
Output:

19 is a Lucky Number.

Method-2: Java Program to Check Lucky Number By User Input Value

import java.util.*;  
public class Main 
{  
    public static int count = 2;  
    
    //checkNumber() user defined method 
    //To check if it is a lucky  number or not
    static boolean checkNumber(int num)  
    {  
        //means if number less than 2 
        //then considered as lucky number
        //Hence return true
        if(count > num)  
            return true;
            
        //Divide num with count value
        //If the number is divisible
        //then return false
        if(num%count == 0)  
            return false; 
            
        //here we are getting the position 
        //of the given number  
        num = num-(num/count);  
        
        //incrementing the count value by 1
        count++;  
        return checkNumber(num);  
    } 
    
    //Main method
    public static void main (String args[])  
    {  
        Scanner sc=new Scanner(System.in);  
        System.out.println("Enter the number : ");  
        //Taking a number input from user
        int num=sc.nextInt();
        //Within the if condition checkNumber() method called
        //if the method returns value as true then it is lucky number.
        if(checkNumber(num))  
            System.out.println(num+" is a Lucky Number."); 
        //Else it is not a lucky number
        else  
            System.out.println(num+" is not a Lucky Number.");  
    }  
}
Output:

Case-1
Enter the number : 19
19  is not a Lucky Number.

Case-2
Enter the number : 5
5 is not a Lucky Number.

Are you new to the java programming language? We recommend you to ace up your practice session with these Basic Java Programs Examples

Related Java Programs: