Skip to content

Instantly share code, notes, and snippets.

@aadipoddar
Created March 31, 2022 15:01
Show Gist options
  • Save aadipoddar/41e35061c06ece1069536838fc2dc78b to your computer and use it in GitHub Desktop.
Save aadipoddar/41e35061c06ece1069536838fc2dc78b to your computer and use it in GitHub Desktop.
Generate and print lucky Number for a Given Number N.
/*
Generate and print lucky Number for a Given Number N
INPUT: 25
OUTPUT:
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
1 3 5 7 9 11 13 15 17 19 21 23 25... eleminate the 2nd number from the list
1 3 7 9 13 15 19 21 25... eleminate the 3rd number from the list
1 3 7 13 15 19 25... eleminate the 4th number from the list
1 3 7 13 19 25... eleminate the 5th number from the list
1 3 7 13 19... eleminate the 6th number from the list
Lucky Number :- 1 3 7 13 19
*/
import java.util.Scanner;
class LuckyNumber {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Input the Number");
int number = sc.nextInt();
int[] arr = new int[number];
int[] ar = new int[number];
for (int i = 0; i < number; i++) {
arr[i] = i + 1;
System.out.print(arr[i] + " ");
}
System.out.println();
int k = 0;
int numberToBeRemoved = 2;
int len = number;
while (len >= numberToBeRemoved) {
for (int i = 1; i <= number; i++) {
if (i % numberToBeRemoved == 0)
arr[i - 1] = 0;
else {
ar[k] = arr[i - 1];
k++;
}
}
k = 0;
for (int i = 0; i < number; i++) {
arr[i] = ar[i];
if (arr[i] != 0) {
System.out.print(arr[i] + " ");
k++;
}
ar[i] = 0;
}
len = k;
k = 0;
System.out.println();
numberToBeRemoved++;
}
System.out.println("Lucky Numbers :- ");
for (int i = 0; i < number; i++)
if (arr[i] != 0)
System.out.print(arr[i] + " ");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment