public class GCD { //main method public static void main(String[] args) { //call
ID: 3534859 • Letter: P
Question
public class GCD
{
//main method
public static void main(String[] args)
{
//calling the GCD method to compute GCD value
try
{
System.out.println("gcd(24,16) is: "+GCD(24,16));
System.out.println("gcd(255,25) is: "+GCD(255,25));
}
//catching the exception
catch(Exception e)
{
System.out.println("Exception has occurred in the class. Program will exit. ");
//Exiting from system if exception occurs
System.exit(0);
}
}
//method to compute GCD value
public static long GCD(long m, long n)
{
//Base Condition
if(n==0)
return m;
//Iterative Condition
else
return GCD(n, m%n);
}
}Type your question here
Explanation / Answer
import java.util.Scanner;
public class GCD
{
 //main method
 public static void main(String[] args)
 {
System.out.println("Enter the Numbers ");
Scanner in = new Scanner(System.in);
int m = in.nextInt();
int n = in.nextInt();
   //calling the GCD method to compute GCD value
   try
   {
     System.out.println("gcd("+m+","+n+") is: "+GCD(m,n));
   }
   //catching the exception
   catch(Exception e)
   {
     System.out.println("Exception has occurred in the class. Program will exit. ");
     //Exiting from system if exception occurs
     System.exit(0);
   }
 }
 //method to compute GCD value
 public static long GCD(long m, long n)
 {
   //Base Condition
   if(n==0)
     return m;
   //Iterative Condition
   else
     return GCD(n, m%n);
 }
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.