Java Program find the HCF of two numbers using recursion

import java.util.*;
class HCF
{
public static void main(String args[])
{
int a,b,i,hcf=0;
System.out.println("enter the numbers");
Scanner sc= new Scanner(System.in);
a=sc.nextInt();
b=sc.nextInt();
/* logic for using simple loop
 p=a*b;
 for( i=1;i<=p;i++)
 {
 if (a%i==0&&b%i==0)
     hcf=i;
}
*/
hcf=findHcf(a,b);
System.out.println("the hcf is"+hcf);
}

static int findHcf(int n1,int n2)
{
if (n2 != 0)
return findHcf(n2, n1 % n2);
else
return n1;
}

}

Video/ C Introduction

Watch video in full size
Wordpress Social Share Plugin powered by Ultimatelysocial