Guest User

Untitled

a guest
Dec 14th, 2018
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.39 KB | None | 0 0
  1. import java.io.*;
  2.  
  3. class EuclidApp {
  4.  
  5. static int[] sides;
  6.  
  7. public static void main(String[] args) throws Exception {
  8. System.out.print("Enter two side lengths separated by a space: ");
  9. System.out.flush();
  10.  
  11. sides = getIntArray();
  12.  
  13. int answer = Euclid(sides[0], sides[1]);
  14.  
  15. System.out.println(answer);
  16. }
  17.  
  18. public static int Euclid(int length, int width){
  19.  
  20. //First find the longer side
  21. if(length > width) {
  22.  
  23. //If there's no remainder, the LCD is width
  24. if((length % width) == 0){
  25. return width;
  26. }
  27. else {
  28. //Get the remainder to the nearest integer
  29. int lengthRemainder = (length % width);
  30.  
  31. return Euclid(width, lengthRemainder);
  32. }
  33. }
  34. else {
  35. if((width % length) == 0) {
  36. return length;
  37. }
  38. else {
  39. int widthRemainder = (width % length);
  40. return Euclid(length, widthRemainder);
  41. }
  42. }
  43. }
  44.  
  45. public static int[] getIntArray() throws IOException{
  46.  
  47. InputStreamReader isr = new InputStreamReader(System.in);
  48. BufferedReader br = new BufferedReader(isr);
  49.  
  50. String input = br.readLine();
  51.  
  52. //Split into String array by space
  53. String[] token = input.split(" ");
  54.  
  55. //Create int array
  56. int[] ints = new int[token.length];
  57.  
  58. //Store strings in int array
  59. for(int i=0; i<token.length; i++){
  60. ints[i] = Integer.parseInt(token[i]);
  61. }
  62. return ints;
  63. }
  64. }
Add Comment
Please, Sign In to add comment