Greatest common divisor and least common multiple of two integers

Source: Internet
Author: User
Tags greatest common divisor

Enter two positive integers m and N to find their greatest common divisor and least common multiple.

Method One:

public class Zuidaogongyueshuyuzuixiaogongbeishu {

public static void Main (string[] args) {

Scanner scanner=new Scanner (system.in);
System.out.println ("Please enter the first integer m:");
int M=scanner.nextint ();
System.out.println ("Please enter a second integer n:");
int N=scanner.nextint ();

int max = (M > N)? m:n;
int min = (M < n)? m:n;

Greatest common divisor
for (int i =min;i >= 1;i--) {
If (m% i = = 0 && n% i ==0) {
System.out.println (i);
Break
}
}

Least common multiple
for (int i = Max;i <= m * n;i++) {
if (i% m = = 0 && i% n = = 0) {
System.out.println (i);
Break
}
}

}

}

Method Two:

/** in the loop, as long as the divisor is not equal to 0, with a larger number divided by the smaller number, a small number as the next round of the large number of cycles, the remainder as the next round of the cycle of the smaller number, so loop until the value of the smaller number is 0, return a larger number, this number is greatest common divisor, Least common multiple is the sum of two numbers divided by greatest common divisor. * /

public class Lianxi06 {
public static void Main (string[] args) {
int A, b,m;
Scanner s = new Scanner (system.in);
System.out.print ("Type an integer:");
A = S.nextint ();
System.out.print ("Re-type an integer:");
b = S.nextint ();
Deff cd = new Deff ();
m = Cd.deff (A, b);
int n = a * b/m;
System.out.println ("Greatest common divisor:" + m);
SYSTEM.OUT.PRINTLN ("Least common multiple:" + N);
}
}
Class deff{
public int Deff (int x, int y) {
int t;
if (x < y) {
t = x;
x = y;
y = t;
}
while (Y! = 0) {
if (x = = y) return x;
else {
int k = x% y;
x = y;
y = k;
}
}
return x;
}
}

or write this:

Class test{
public static void Main (string[] args) {
System.out.print ("Please enter two positive integers:");
Scanner scan = new Scanner (system.in);
int a = Scan.nextint ();
int b = Scan.nextint ();

if (a < b)//Make sure A is a large number
A = (A + b)-(b = a);

int m = A;
int n = b;

While (a% b! = 0) {
int iTemp = A;
A = b;
b = iTemp% B;
}

The greatest common divisor of System.out.println (M + "and" + N + ") are:" + B ";
SYSTEM.OUT.PRINTLN (M + "and" + N + "least common multiple are:" + m*n/b);
}
}

Greatest common divisor and least common multiple of two integers

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.