Greatest Common Divisor or GCD using Recursion in java


public class GCD
{
    public static int gcd(int num1, int num2)
    {
        if(num2 == 0)
        {
            return num1;
        }
        else
        {
            return gcd(num2, num1%num2);
        }
    }

}

//main class


import java.util.Scanner;

public class Main
{

    public static void main(String[] args)
    {
       Scanner input = new Scanner(System.in);

      System.out.print("Enter the first number: ");
      int num1 = input.nextInt();

      System.out.print("Enter the second power: ");
      int num2 = input.nextInt();

      GCD access = new GCD();

      System.out.print("The GCD of 2 numbers is: " + access.gcd(num1, num2));

    }

}

0 comments:

Post a Comment

 
 
 
 


Copyright © 2012 http://codeprecisely.blogspot.com. All rights reserved |Term of Use and Policies|