Write a recursive program in ‘C’ to find the L.C.M. (Least Common Multiple) of two given numbers. 10m Dec2006
#include<stdio.h>
int lcm(int,int);
void main()
{
int NUM1,NUM2,LCM;
clrscr();
printf(“ENTER ANY TWO POSITIVE NUMBERS TO FIND ITS L.C.M. : “);
scanf(“%d%d”,&NUM1,&NUM2);
if(NUM1>NUM2)
LCM = lcm(NUM1,NUM2);
else
LCM = lcm(NUM2,NUM1);
printf(“LCM OF TWO NUMBERS IS %d”,LCM);
getch();
}
int lcm(int N1,int N2)
{
static int TEMP = 1;
if(TEMP % N2 == 0 && TEMP % N1 == 0)
return TEMP;
TEMP++;
lcm(N1,N2);
return TEMP;
}
[codesyntax lang=”c”]
#include<stdio.h> int lcm(int,int); void main() { int NUM1,NUM2,LCM; clrscr(); printf("ENTER ANY TWO POSITIVE NUMBERS TO FIND ITS L.C.M. : "); scanf("%d%d",&NUM1,&NUM2); if(NUM1>NUM2) LCM = lcm(NUM1,NUM2); else LCM = lcm(NUM2,NUM1); printf("LCM OF TWO NUMBERS IS %d",LCM); getch(); } int lcm(int N1,int N2) { static int TEMP = 1; if(TEMP % N2 == 0 && TEMP % N1 == 0) return TEMP; TEMP++; lcm(N1,N2); return TEMP; }
[/codesyntax]
Screen Shots: