sandeshMC

GCD using Euclidean Theorem

Apr 5th, 2014
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.68 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class EuclideanTheorem {
  4.  
  5.     /**
  6.      * @param args
  7.      */
  8.     public static void main(String[] args) {
  9.         // TODO Auto-generated method stub
  10.         Scanner sc = new Scanner(System.in);
  11.         System.out.println("Enter two numbers to compute GCD : ");
  12.         int x = sc.nextInt();
  13.         int y = sc.nextInt();
  14.  
  15.         outer: while (true) {
  16.             if (x > y) {
  17.                 x %= y;
  18.                 if (x == 0) {
  19.                     System.out.println("THE GCD is :" + y);
  20.                     break outer;
  21.                 }
  22.             } else {
  23.                 y %= x;
  24.                 if (y == 0) {
  25.                     System.out.println("THE GCD is :" + x);
  26.                     break outer;
  27.                 }
  28.             }
  29.         }
  30.     }
  31.  
  32. }
  33.  
  34. /*
  35. OUTPUT :
  36. Enter two numbers to compute GCD :
  37. 8765
  38. 23485
  39. THE GCD is :5
  40. */
Advertisement
Add Comment
Please, Sign In to add comment