Tuesday 13 November 2012

How to write C Program to calculate L.C.M and G.C.D of two numbers using function


Program source code
.first of all create two function first function gcd to find out gcd() and next lcm().
we need to pass two parameter  in given function .

void gcd(int,int); // created function for gcd
void lcm(int,int); // created function for lcm
int main()
{
int a,b;
printf("Enter two numbers: \t");
scanf("%d %d",&a,&b);
lcm(a,b);
gcd(a,b);
return 0;
}


//function to calculate l.c.m
LCM=Least Common Multiple or lowest common multiple

How to find  LCM of 3 and 5 by manually  .
Multiples of 3 are 3,6,9,12,15,18 etc..


and the multiples of 5 are 5,10,15,20, etc... 15 is common in both case.
so lcm of 3 and 5 is 15.

void lcm(int a,int b)
{
int m,n;
m=a;
n=b;
while(m!=n)
{
if(m
m=m+a;
else
n=n+b;
}
printf("\nL.C.M of %d & %d is %d",a,b,m);
}

//function to calculate g.c.d
GCD =Greatest common divisor , One number divides two number like
12  and 20 is divisible by 4 ,
hence gcd of 12 and 20 is 4.
void gcd(int a,int b)
{
int m,n;
m=a;
n=b;
while(m!=n)
{
if(m>n)
m=m-n;
else
n=n-m;
}
printf("\nG.C.D of %d & %d is %d",a,b,m);
Share This
Previous Post
Next Post

FYJC XI standard online admisson Process and declaraton of Merit list . Cut off List For prevous year also . 10 Th Results onlne declaraton Maharashtra Region .

0 comments: