404 遞迴最大公因數

import java.util.Scanner;
public class JPA404 {
    static Scanner keyboard = new Scanner(System.in);
    public static void main(String args[]) {
int m;
int n;
      System.out.print("Input m : ");
      m=keyboard.nextInt();
     
      while(m!=999){

      System.out.print("Intput n :");
      n=keyboard.nextInt();
      System.out.println("最大公因數 : "+fact(m,n));
System.out.print("Input m : ");
      m=keyboard.nextInt();
      }
    }

    static int fact (int m,int n){
    if(n==0){
    return m;
  }
  else{
  return fact(n,m%n);
  }
    }

}

沒有留言:

張貼留言