Java program design: The maximum common divisor and the minimum common multiple.

Source: Internet
Author: User

Java program design: The maximum common divisor and the minimum common multiple.

Question: enter two positive integers, number1 and number2, to obtain the maximum common number and the minimum common number.

Algorithm: returns the remainder of a large number and a decimal number. The remainder of a decimal number is obtained until the remainder is 0, which is the maximum common divisor (moving phase division ); the maximum public factor numbe1 * number2/(the maximum public approx.). Run the following code:

Import java. util. secret; public class Max_Min {static int n1; public static void main (String [] args) {Max_Min m = new Max_Min ();/** enter two values here, obtain the minimum public multiple and maximum public approx. */Scanner s = new limit (System. in); System. out. print ("enter a positive Integer:"); int number1 = Integer. parseInt (s. nextLine (); System. out. print ("enter another positive Integer:"); int number2 = Integer. parseInt (s. nextLine (); if (number1> number2) {// here, if is used to filter out a large number. In the function, use m to obtain the remainder. function (number1, number2);} else {m. function (number2, number1);} m. function2 (number1, number2, n1); // This method returns the smallest public multiple} public void function (int number1, int number2) {if (number1% number2 = 0) {System. out. println ("maximum common approx.:" + number2); getInt (number2); // pass the maximum common approx. Value} else {function (number2, number1 % number2 );}} public void getInt (int number) {// obtain the maximum public approx. this. n1 = number;} public void function2 (int number1, int number2, int number) {// output the maximum public multiple System. out. println ("the maximum public factor is:" + number1 * number2/number );}}

 

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.