Guest User

Untitled

a guest
Jul 22nd, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.12 KB | None | 0 0
  1. //Angelu Ferdinand A. Garcia
  2. //CC11 - A
  3. //July 22, 2018
  4. //This program outputs the least common multiple, LCM, of two positive integers input from user, x & y.
  5.  
  6. import java.util.Scanner;
  7. public class lcmprogram
  8. {
  9.     public static void main (String args[])
  10.     {
  11.         Scanner sc1 = new Scanner(System.in);
  12.         System.out.print("Enter the first positive integer: ");
  13.         int x = sc1.nextInt();
  14.         Scanner sc2 = new Scanner(System.in);
  15.         System.out.print("Enter the second positive integer: ");
  16.         int y = sc2.nextInt();
  17.  
  18.         int LCM = 0;
  19.  
  20.         if (x<=0 || y<=0)
  21.             System.out.println("Error: input must be a positive integer.");
  22.         else
  23.         {
  24.             if (x==y)
  25.                 LCM=x;
  26.             else if (x>y)
  27.                 LCM=x;
  28.             else
  29.                 LCM=y;
  30.            
  31.             while (!(LCM%x==0 && LCM%y==0)) //if remainder of LCM to x and y is not equal to zero.
  32.                 {
  33.                     LCM++;
  34.                 }
  35.                 System.out.println("Least Common Multiple: "+LCM);
  36.         }
  37.     }            
  38.  
  39. }
Advertisement
Add Comment
Please, Sign In to add comment