Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
public class Multiples {
public static void main(String[] args){
Queue<Integer> multiples = new LinkedList<Integer>();
Scanner scan = new Scanner(System.in);
// Get Input
System.out.print("Enter first number: ");
int num1 = scan.nextInt();
System.out.print("Enter second number: ");
int num2 = scan.nextInt();
// Check Multiples
int max = num1 * num2;
for(int i = 1; num1 * i <= max; i++){
if((num1 * i) % num2 == 0) multiples.add(num1 * i);
}
// Print Multiples
System.out.print("Common Multiples: ");
int lcm = multiples.remove();
int queueSize = multiples.size();
for(int i = 0; i < queueSize; i++) System.out.print(multiples.remove() + " ");
System.out.println("\nLeast Common Multiple: " + lcm);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.