Q:

Java - Greatest Common Divisor or Euclidean Algorithm Program or Highest Common Divisor

0

Java - Greatest Common Divisor or Euclidean Algorithm Program or Highest Common Divisor

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Greatest Common Divisor / Euclidean Algorithm Program / Highest Common Divisor

public class Gcd {
    //  greatest common divisor
    public static int gcd(int First_number, int Second_number) {
        int i = First_number % Second_number;
            while (i != 0) {
            First_number = Second_number;
            Second_number = i;
            i = First_number % Second_number;
        }
        return Second_number;
    }
  
    public static void main(String[] args) {     
        Scanner sc=new Scanner(System.in);
 
        System.out.print("First Number  :");
        int num1=sc.nextInt();
        System.out.print("Second Number :");
        int num2=sc.nextInt();
 
        System.out.println("Greatest Common Divisors: " + gcd(num1,num2));
       
    }
}

Output

    First Number  :24
    Second Number :16
    Greatest Common Divisors: 8

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now