Class 11 Class 12

Find HCF and LCM using a Recursive Function in Java

Write a program to input two integers from the user and calculate and display the HCF or GCD and LCM using a recursive method.

HCF or GCD is the highest common factor for the given integers.

LCM is the least common multiple for the given integers.

Recursive Function or Method is a function that calls itself.

class Solve{
    public static void main(String args[])throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(;
        System.out.print("First number: ");
        int a = Integer.parseInt(br.readLine());
        System.out.print("Second number: ");
        int b = Integer.parseInt(br.readLine());
        int hcf = gcd(a, b);
        int lcm = a * b / hcf;
        System.out.println("HCF: " + hcf);
        System.out.println("LCM: " + lcm);
    public static int gcd(int p, int q){
        if(q == 0)
            return p;
        return gcd(q, p % q);

2 replies on “Find HCF and LCM using a Recursive Function in Java”

Sir, program is given which states that:
The feint distance between 2 strings is calculated by counting the number of corresponding characters that are unequal.
Eg: Feint distance between “abcde” and “Thin pbcq” is 1+0+0+1+1=3
Class name:Feint
Data Members:
s1,s2:to input 2 strings
fd: integer to store feint distance
Member methods:
Feint():constructor to initialise the data members to null
void fnInput():to input the 2 strings
void fnSetFeint():to calculatethe Feint distance between the member string s1 & s2 and store in data member fd.
void fnShow():to display the data members with appropriate message.
Specify the class Feint giving details of the constructor,void fnInput(), void fnSetFeint() and void fnShow(). Define a main() function to create an object and call the functions accordingly to enable the task.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.