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