Compute GCD using three different methods in Java

Problem:

Compute GCD using three different methods in Java.

Output:

Not applicable.

Solution:

/**--------------------Method 1 ---------------*/
public class gcd_recursion_azzam
{
   public static int gcd(int m, int n, int i ) {
     
       if (m%i ==0 && n %i ==0)
         return i;
       else
         return gcd(m,n,--i);
  }
   
   public static void main(String[] args)
   {
     System.out.println(gcd(10,4,4));
   }
}
/**--------------------Method 2 ---------------*/
public class gcd_recursion_book
{
   public static int gcd(int m, int n) {
     
       if (m % n == 0) 
         return n;
       else if (m < n)
         return gcd(m,n);
       else
         return gcd(n, m%n);
     } 
   
   public static void main(String[] args)
   {
     System.out.println(gcd(10,4));
   }
}
/**--------------------Method 3 ---------------*/
public class gdc_recursion_online
{
   public static int gcd(int a, int b) {
     
       if (b==0) 
         return a;
       else
         return gcd(b, a % b);
     } 
   
   public static void main(String[] args)
   {
     System.out.println(gcd(10,4));
   }
}


1 comment :

  1. laser liposuction Korea's #1 Liposculpture Clinic. Lydian plastic surgery is the home of VIP patients. Celebrities, Influencers and Diplomats all know and trust Doctor An and Lydian plastic surgery clinic to provide detailed results.

    ReplyDelete

Follow Me

If you like our content, feel free to follow me to stay updated.

Subscribe

Enter your email address:

We hate spam as much as you do.

Upload Material

Got an exam, project, tutorial video, exercise, solutions, unsolved problem, question, solution manual? We are open to any coding material. Why not upload?

Upload

Copyright © 2012 - 2014 Java Problems  --  About  --  Attribution  --  Privacy Policy  --  Terms of Use  --  Contact