Compute GCD using three different methods in Java

Problem:

Compute GCD using three different methods in Java.

Output:

Not applicable.

Solution:

01/**--------------------Method 1 ---------------*/
02public class gcd_recursion_azzam
03{
04   public static int gcd(int m, int n, int i ) {
05      
06       if (m%i ==0 && n %i ==0)
07         return i;
08       else
09         return gcd(m,n,--i);
10  }
11    
12   public static void main(String[] args)
13   {
14     System.out.println(gcd(10,4,4));
15   }
16}
17/**--------------------Method 2 ---------------*/
18public class gcd_recursion_book
19{
20   public static int gcd(int m, int n) {
21      
22       if (m % n == 0)
23         return n;
24       else if (m < n)
25         return gcd(m,n);
26       else
27         return gcd(n, m%n);
28     }
29    
30   public static void main(String[] args)
31   {
32     System.out.println(gcd(10,4));
33   }
34}
35/**--------------------Method 3 ---------------*/
36public class gdc_recursion_online
37{
38   public static int gcd(int a, int b) {
39      
40       if (b==0)
41         return a;
42       else
43         return gcd(b, a % b);
44     }
45    
46   public static void main(String[] args)
47   {
48     System.out.println(gcd(10,4));
49   }
50}


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