130. JAVA Program to find GCD of n numbers using recursion.
How does this program work?
- This program is used to find GCD of n numbers using recursion in java.
- GCD or HCF (Highest Common Factor) of numbers is the largest number that divides by them.
Here is the code
//To find GCD of n numbers using recursion
import java.util.Scanner;public class number
{
public static void main(String[] args )
{
System.out.println("Enter first number :: " );
Scanner obj= new Scanner(System.in );
int firstnum = obj.nextInt();
System.out.println("Enter second number :: " );
int secondnum = obj.nextInt();
System.out.println("GCD of given two numbers is :"+gcd(firstnum, secondnum));
}
public static int gcd(int num1, int num2 )
{
if (num2 != 0)
{
return gcd(num2, num1 % num2);
}
else
{
return num1;
}
}
}