C Program GCD and LCM MCA Sem 1

This C program finds the Greatest Common Divisor (GCD) and Least Common Multiple (LCM)of 2 given numbers. This is a part of Mumbai University MCA Colleges C Programs MCA Sem 1

#include<stdio.h>
#include<conio.h>
#include<math.h>

void main()
{
int a,b,c,n1,n2,gcd,lcd;
clrscr();
printf("\nEnter number 1:");
scanf("%d",&n1);
printf("\nEnter number 2:");
scanf("%d",&n2);
if(n1>n2)
{
a=n2;
b=n1;
}
else
{
a=n1;
b=n2;
}
while(a>0)
{
c=b%a;
if(c==0)
{
gcd=a;
break;
}
b=a;
a=c;
}
lcd=(n1*n2)/gcd;
printf("\nLCM is: %d",lcd);
printf("\nGCD is: %d",gcd);
getch();
}

Hope this Program is useful to you in some sense or other. Keep on following this blog for more Mumbai University MCA College Programs. Happy Programming and Studying.

No comments:

Post a Comment