Program to find LCM of two numbers using recursion


Default
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
//Write a program to find lcm of given two numbers using recursion
//Eg: LCM(2,4)=4 , LCM(6,8)=24

 
#include <stdio.h>
int lcm(int ,int );
int l=1;
int main()
{
    int n1,n2;
    printf("Enter first number:");
    scanf("%d",&n1);
    printf("Enter second number:");
    scanf("%d",&n2);
    if(n1<=0 || n2<=0)
        printf("Invalid inputs...\n");
    else
        printf("LCM of %d and %d is %d\n",n1,n2,lcm(n1,n2));
    return 0;
}
int lcm(int x,int y)
{
    if (l%x==0 && l%y==0)
        return l;
    else{
        l++;
        return lcm(x,y);
}
}

No comments:

Post a Comment