The greatest common divisor and least common multiple of Java programming

Source: Internet
Author: User
Tags greatest common divisor

Title: Enter two positive integers number1 and number2, seeking their greatest common divisor and least common multiple.

Algorithm: The larger number and the smaller number take the remainder, the smaller number except the remnant, until the remainder is 0 o'clock, is greatest common divisor (the division method); Max common multiple numbe1*number2/(greatest common divisor), directly below the code:

ImportJava.util.Scanner; Public classMax_min {Static intN1;  Public Static voidMain (string[] args) {max_min m=Newmax_min (); /** Enter two values here to find least common multiple and greatest common divisor*/Scanner S=NewScanner (system.in); System.out.print ("Please enter a positive integer:"); intNumber1 =Integer.parseint (S.nextline ()); System.out.print ("Please enter a positive integer:"); intNumber2 =Integer.parseint (S.nextline ()); if(Number1>number2) {//If the judgment here is to filter out the larger number, the function function takes the remainder to usem.function (NUMBER1,NUMBER2); }            Else{m.function (number2, number1); } m.function2 (Number1, number2,n1); //This method asks least common multiple    }         Public voidfunctionintNumber1,intnumber2) {        if(Number1%number2 = = 0) {System.out.println ("Greatest common divisor for:" +number2);  GetInt (NUMBER2); //Pass the value of greatest common divisor}Else{function (number2, Number1%number2); }    }         Public voidGetInt (intNumber) {//get greatest common divisor         This. N1 =Number ; }         Public voidFunction2 (intNumber1,intNumber2,intNumber) {//output Max Common multipleSYSTEM.OUT.PRINTLN ("Max Common multiple is:" +number1*number2/Number ); }}

Greatest common divisor and least common multiple of Java programming

Related Article

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.