Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- class EuclidApp {
- static int[] sides;
- public static void main(String[] args) throws Exception {
- System.out.print("Enter two side lengths separated by a space: ");
- System.out.flush();
- sides = getIntArray();
- int answer = Euclid(sides[0], sides[1]);
- System.out.println(answer);
- }
- public static int Euclid(int length, int width){
- //First find the longer side
- if(length > width) {
- //If there's no remainder, the LCD is width
- if((length % width) == 0){
- return width;
- }
- else {
- //Get the remainder to the nearest integer
- int lengthRemainder = (length % width);
- return Euclid(width, lengthRemainder);
- }
- }
- else {
- if((width % length) == 0) {
- return length;
- }
- else {
- int widthRemainder = (width % length);
- return Euclid(length, widthRemainder);
- }
- }
- }
- public static int[] getIntArray() throws IOException{
- InputStreamReader isr = new InputStreamReader(System.in);
- BufferedReader br = new BufferedReader(isr);
- String input = br.readLine();
- //Split into String array by space
- String[] token = input.split(" ");
- //Create int array
- int[] ints = new int[token.length];
- //Store strings in int array
- for(int i=0; i<token.length; i++){
- ints[i] = Integer.parseInt(token[i]);
- }
- return ints;
- }
- }
Add Comment
Please, Sign In to add comment