sandeshMC

Compute GCD

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