본문 바로가기
공부

최대공약수

by 하프상 2017. 3. 25.

import java.util.Scanner;


public class homework {


public static void main(String[] args) {

// TODO Auto-generated method stub


Scanner sc = new Scanner(System.in);

int a = sc.nextInt();

Scanner sc2= new Scanner(System.in);

int b = sc.nextInt();

int res = 0;

if(a>b)

{

for(int i=1; i<=b; i++)

{

if(a%b == 0)

{

System.out.println(b);

return;

}

else if(a%b != 0)

{

if(b%i == 0)

{

if(a%i == 0)

{

res = i;

}

}

}

}

System.out.println(res);

}

else if(a<b)

{

for(int i=1; i<=a; i++)

{

if(b%a == 0)

{

System.out.println(a);

return;

}

else if(b%a != 0)

{

if(a%i == 0)

{

if(b%i == 0)

{

res = i;

}

}

}

}

System.out.println(res);

}

else

{

System.out.println(a);

}

}


}



댓글