Java語言程式設計(基礎篇)第十版 5.14

炸醬麵賣完了發表於2018-10-12
public class J5_14 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		java.util.Scanner input = new java.util.Scanner(System.in);
		System.out.print("Enter number of n1 and n2:");
		int n1 = input.nextInt();
		int n2 = input.nextInt();
		
		int d = 0;
		if(n1 >n2)
			d = n2;
		else
			d = n1;
		
		int gcd =0;
		
		
		do {
			if(n1 %d ==0 && n2 %d ==0)
			{
				gcd =d;
				break;
			}
			else
				d--;
		}while(d!=0);

		System.out.println("n1 and n2 gcd is :"+gcd);
		
	}

}

相關文章