It seems to run - I enter 2 lines on the keyboard and if they are not positive integers, an Exception is thrown - otherwise the GCD is displayed.
I am really stuck on this - did I misunderstand the requirements?
package com.codegym.task.task14.task1420;
/*
GCD
*/
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Solution {
public static void main(String[] args) throws Exception {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String one = reader.readLine();
String two = reader.readLine();
int input1 = Integer.parseInt(one);
int input2 = Integer.parseInt(two);
if(input1 > 0 && input2 > 0){ printGCD(input1, input2);}
else{throw new NumberFormatException("Number needs to be positive");}
}
public static void printGCD(int input1, int input2) {
int a = 0;
int b = 0;
if (input1 > input2) {
a = input1;
b = input2;
} else if (input2 > input1) {
a = input1;
b = input2;
}
int rest = 0;
while (true) {
rest = a % b;
if (rest == 0) {
System.out.println(b);
break;
} else if (rest > 0) {
a = b;
b = rest;
}
}
}
}