packagefirst;importjava.util.Scanner;/**求两个数的最大公约数和最小公倍数*/publicclassYueshuBeishu{publicstaticvoidmain(String[]args){Scannerscanner=newScanner(System.in);System.out.println("请输入第一个数:");intnum1=scanner.nextInt();System.out.println("请输入第二个数:");intnum2=scanner.nextInt();intbig=big(num1,num2);intsmall=small(num1,num2);if(big==0||small==0){System.out.println("求最大公约数时,除数不能为0");}else{System.out.println(num1+"和"+num2+"的最大公约数是:"+big);
System.out.println(num1+"和"+num2+"的最小公倍数是:"+small);}}//求最大公约数:较大的数除以较小的数,然后将较小的数作为下一次的大数,余数作为小数,直到较小的数为0,返回较大的数publicstaticintbig(intnum1,intnum2){intbig=0;intsmall=0;if(num1>num2){big=num1;small=num2;}else{big=num2;small=num1;}if(small==0){System.out.println("除数不能为0");return0;}while(true){if(small==0){
returnbig;}else{inttemp=big;big=small;small=temp%small;}}}//求最小公倍数:两数之积除以最大公约数publicstaticintsmall(intnum1,intnum2){intbig=big(num1,num2);if(big==0){System.out.println("求最大公约数时,除数不能为0");return0;}else{return(num1*num2)/big;}}}