Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- class Problem1{
- public static void main( String[] args ){
- Scanner sc = new Scanner(System.in);
- int num1;
- int num2;
- int dividend = 0;
- int divisor = 0;
- int remainder = 0;
- int remainder1 = 0;
- int check1;
- int check2;
- do {
- System.out.println("Enter a positive integer: ");
- while (!sc.hasNextInt()) {
- System.out.println("Please enter a positive integer:");
- sc.next();
- }
- num1 = sc.nextInt();
- } while (num1 <= 0);
- {
- do {
- System.out.println("Enter a positive integer: ");
- while (!sc.hasNextInt()) {
- System.out.println("Please enter another positive integer:");
- sc.next();
- }
- num2 = sc.nextInt();
- } while (num2 <= 0);
- {
- }
- if(num1 > num2){
- dividend = num1;
- divisor = num2;
- remainder = num1 % num2;
- if (remainder == 0){
- System.out.println("The GCD of " + num1 + " and " + num2 + " is " + num2);
- System.exit(0);
- }
- } else if(num2 > num1) {
- divisor = num1;
- dividend = num2;
- remainder = num2 % num1;
- if (remainder == 0){
- System.out.println("The GCD of " + num1 + " and " + num2 + " is " + num1);
- System.exit(0);
- }
- }else if(num2 == num1) {
- System.out.println("The GCD of " + num1 + " and " + num2 + " is " + num1);
- System.exit(0);
- }
- while(remainder > 0){
- dividend = divisor;
- divisor = remainder;
- remainder1 = remainder;
- remainder = dividend % divisor;
- }
- System.out.println("The GCD of " + num1 + " and " + num2 + " is " + remainder1);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement