Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Angelu Ferdinand A. Garcia
- //CC11 - A
- //July 22, 2018
- //This program outputs the least common multiple, LCM, of two positive integers input from user, x & y.
- import java.util.Scanner;
- public class lcmprogram
- {
- public static void main (String args[])
- {
- Scanner sc1 = new Scanner(System.in);
- System.out.print("Enter the first positive integer: ");
- int x = sc1.nextInt();
- Scanner sc2 = new Scanner(System.in);
- System.out.print("Enter the second positive integer: ");
- int y = sc2.nextInt();
- int LCM = 0;
- if (x<=0 || y<=0)
- System.out.println("Error: input must be a positive integer.");
- else
- {
- if (x==y)
- LCM=x;
- else if (x>y)
- LCM=x;
- else
- LCM=y;
- while (!(LCM%x==0 && LCM%y==0)) //if remainder of LCM to x and y is not equal to zero.
- {
- LCM++;
- }
- System.out.println("Least Common Multiple: "+LCM);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment