Lucky Numbers : Java : BlueJ

Objective :


The class LuckyNumbers takes input of the Array of N Elements and asks the Position of Starting deletion. As the Program Progresses, After Each Sequence, the the Deletion Count Increases by 1 thus the Numbers which Remained at Last are known as Lucky Numbers. For Example : val = 1-10 delete position = 2

1 2 3 4 5 6 7 8 9 10 // deleted at 2

1 3 5 7 9 // deleted at 3

1 3 7 9 //deleted at 4

1 3 7 //LUCKY NUMBERS


BlueJ Program Screenshot :



Java Program Source Code :

/**
 * The class LuckyNumbers takes input of the Array of N Elements and asks the
 * Position of Starting deletion. As the Program Progresses, After Each Sequence,
 * the the Deletion Count Increases by 1 thus the Numbers which Remained at Last
 * are known as Lucky Numbers.
 * @author SHANTANU KHAN
 * @mail shantanukhan1995@gmail.com
 * @website 0code.blogspot.com
 * @Program Type : BlueJ Program - Java
 */
import java.io.*;
public class LuckyNumbers
{
    static BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    static int O[],delete=0,flag=0;
    
    private void input()throws Exception
    {
        System.out.print("Enter the Number of Elements : ");
        O=new int[Integer.parseInt(br.readLine())];
        for(int i=0;i<O.length;i++)
        {
            System.out.print("Enter the Elements No. "+(i+1)+" : ");
            O[i]=Integer.parseInt(br.readLine());
        }
        do
        {
            flag=0;
            System.out.print("\nEnter the Position to start Deletion : ");
            delete=Integer.parseInt(br.readLine());
            if(!((delete>1)&&(delete<O.length)))
            { System.out.println("Invalid Delete Position : > 1 and < "+O.length); flag=1; }
        }while(flag==1);
    }
    
    public void lucky(int O[],int delete)
    {
        print(O);
        int N[],count=0,p=0;
        if(O.length>=delete)
        {
            for(int i=0;i<O.length;i++)
                if((i+1)%delete==0)
                    count++;
            N=new int[O.length-count]; //Re initialization of Array N
            for(int j=0;j<O.length;j++)
                if(!((j+1)%delete==0))
                    N[p++]=O[j];
            lucky(N,(delete+1));
        }
    }
    
    private void print(int A[])
    {
        for(int i=0;i<A.length;i++)
            System.out.print(A[i]+" ");
        System.out.println("\n");
    }
    
    public static void main(String args[])throws Exception
    {
        LuckyNumbers obj=new LuckyNumbers();
        obj.input();System.out.println("\nLucky Number Operation :\n");obj.lucky(O,delete);
    }
}

    


© Shantanu Khan 0code ® Lucky Numbers