计算“两个整数的最大公约数”程序

来源:互联网 发布:手机声音剪辑软件 编辑:程序博客网 时间:2024/05/22 13:32
import java.util.Scanner;
public class gcd {
 public int gcd(int a,int b){
  int c;
  if(a==b){
   return a;
  }
  if(a<b){
   int temp;
   temp=a;
   a=b;
   b=temp;
  }
  while((c=a%b)!=0){
   a=b;
   b=c;
  }
  return b;
 }


public static void main(String[] args) {
 gcd g=new gcd();
 Scanner sc=new Scanner(System.in);
 System.out.println("请输入第一个正整数");
 int NumA=sc.nextInt();
 System.out.println("请输入第二个正整数");
 int NumB=sc.nextInt();
 System.out.println("最大公约数是:"+g.gcd(NumA, NumB));
}

}




0 0
原创粉丝点击