Do I have everything I need for this? It is Java and I need to put it into two f
ID: 3789196 • Letter: D
Question
Do I have everything I need for this? It is Java and I need to put it into two files. What do I put where? Code Below
import java.util.*;
public class GCDLCM// this is GCDLCM class
{
// gcd method
public static int gcd(int x, int y){
if(x<=1||y<=1)
return 1;
while(x!=y){
if(x<y)
y=y-x;
else
x=x-y;
}
return x;
}
// finding lcm method
static int lcm(int x, int y)
{
int greater;
greater = (x > y) ? x : y; // greater number
while(true)
{
if(greater % x == 0 && greater % y == 0)
return greater;
++greater;
}
}
public static void main(String args[])// main method starts here
{
Scanner scan = new Scanner(System.in);
System.out.println("GCD : "+gcd(180,81));
System.out.println("LCM : "+lcm(180,81));
System.out.println("GCD : "+gcd(7622,618));
System.out.println("LCM : "+lcm(7622,618));
System.out.println("GCD : "+gcd(4368,2653));
System.out.println("LCM : "+lcm(4368,2653));
}
}
Explanation / Answer
Here are both the files. create a package with your lastname and then put both the files into it.
GCD.java
package simple;
public class GCD {
// gcd method
public int gcd(int x, int y)
{
if(x<=1||y<=1)
return 1;
while(x!=y){
if(x<y)
y=y-x;
else
x=x-y;
}
return x;
}
// finding lcm method
public int lcm(int x, int y)
{
int greater;
greater = (x > y) ? x : y; // greater number
while(true)
{
if(greater % x == 0 && greater % y == 0)
return greater;
++greater;
}
}
}
AssignGCD.java
package simple;
public class AssignGCD {
public static void main(String[] args) {
GCD gcd_lcm = new GCD();
System.out.println("GCD(180,81)="+gcd_lcm.gcd(180,81));
System.out.println("LCM(180,81)="+gcd_lcm.lcm(180,81));
System.out.println("GCD(7622,618)="+gcd_lcm.gcd(7622,618));
System.out.println("LCM(7622,618)="+gcd_lcm.lcm(7622,618));
System.out.println("GCD(4368,2653)="+gcd_lcm.gcd(4368,2653));
System.out.println("LCM(4368,2653)="+gcd_lcm.lcm(4368,2653));
}
}
OUTPUT:
GCD(180,81)=9
LCM(180,81)=1620
GCD(7622,618)=206
LCM(7622,618)=22866
GCD(4368,2653)=7
LCM(4368,2653)=1655472
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.