Find the LCM of two numbers using recursion

 Problem statement:- Program to Find LCM of two numbers using recursion.

Data requirement:-

   Input Data:- num1, num2

  Output Data:-lcm(num1,num2)

Program in C

Here is the source code of the C Program to Find LCM of two numbers using recursion.

Code:

#include<stdio.h>
int gcd(int num1, int num2)
{
   if(num2==0)
    return num1;
return gcd(num2,num1%num2);
}
int lcm(int num1, int num2)
{
return (num1*num2)/gcd(num1,num2);
}
int main()
{
    int num1,num2;
    printf("Enter the Two Number:");
    scanf("%d%d",&num1,&num2);
    printf("Lcm of Given Number Using Recursion is:%d",lcm(num1,num2));
}

Input/Output:
Enter the Two Number:14 4
Lcm of Given Number Using Recursion is:28

Program in C++

Here is the source code of the C++ Program to Find LCM of two numbers using recursion.

Code:

#include<iostream>
using namespace std;
int gcd(int num1, int num2)
{
   if(num2==0)
    return num1;
return gcd(num2,num1%num2);
}
int lcm(int num1, int num2)
{
return (num1*num2)/gcd(num1,num2);
}
int main()
{
    int num1,num2;
    cout<<"Enter the Two Number:";
    cin>>num1>>num2;
    cout<<"Lcm of Given Number Using Recursion is:"<<lcm(num1,num2);
}

Input/Output:
Enter the Two Number:5 15
Lcm of Given Number Using Recursion is:15

Program in Java

Here is the source code of the Java Program to Find LCM of two numbers using recursion.

Code:

import java.util.Scanner;
public class LcmOfGivenNumbers {
static int gcd(int num1, int num2)
{
   if(num2==0)
    return num1;
return gcd(num2,num1%num2);
}
static int lcm(int num1, int num2)
{
return (num1*num2)/gcd(num1,num2);
}
public static void main(String[] args) {
Scanner cs=new Scanner(System.in);
int num1, num2;
    System.out.print("Enter the two Number:");
    num1=cs.nextInt();
    num2=cs.nextInt();
System.out.print("Lcm of Given Numbers Using Recursion is: "+lcm(num1,num2));
        cs.close();
}
}

Input/Output:
Enter the two Number:30 35
Lcm of Given Numbers Using Recursion is: 210

Program in Python

Here is the source code of the Python Program to Find LCM of two numbers using recursion.

Code:

def gcd(num1,num2):
    if num2==0:
        return num1
    else:
        return gcd(num2,num1%num2)
def lcm(num1,num2):
    return (num1 * num2) // gcd(num1, num2)
print("Enter the two Number:")
num1=int(input())
num2=int(input())
print("Lcm of Given Numbers Using Recursion is:",lcm(num1,num2))

Input/Output:

Post a Comment

0 Comments