JAVA basic programming exercises 6 [program] 6 greatest common divisor and least common multiple

 

6 6 [program] least common multiple and greatest common divisor

Title: Enter two positive integers m and n, and seeking the least common multiple of the greatest common divisor.

Program analysis: the use of rolling division.

 

package cskaoyan;

public class cskaoyan6 {
	@org.junit.Test
	public void gcd_lcm() {
		java.util.Scanner in = new java.util.Scanner(System.in);
		int m = in.nextInt();
		int n = in.nextInt();

		System.out.println(gcd(m, n));
		System.out.println(lcm(m, n));

		in.close();
	}

	public int gcd(int m, int n) {// 最大公约数
		int temp = 0;

		if (m < n) {
			temp = m;
			m = n;
			n = temp;
		}

		while (n != 0) {
			temp = m % n;
			m = n;
			n = temp;
		}

		return m;
	}

	public int lcm(int m, int n) {// 最小公倍数
		int temp;

		temp = m * n / gcd(m, n);

		return temp;
	}
}

 

Guess you like

Origin www.cnblogs.com/denggelin/p/11291990.html