(C++) CPP Programs - 5
Friday, December 14, 2007
5. Write a C program to find the GCD and LCM of 2 integer numbers and output the results along with the given integers. Use
/*GCD and LCM*/
#include
#include
void main()
{
int nr,dr,a,b,lcm,r;
clrscr();
printf("Enter the value for a,b\n");
scanf("%d%d",&a,&b);
if(a>=b)
{
nr=a;
dr=b;
}
else
{
nr=b;
dr=a;
}
r=nr%dr;
while(r!=0)
{
nr=dr;
dr=r;
r=nr%dr;
}
lcm=(a*b)/dr;
printf("GCD of %d & %d = %d\n",a,b,dr);
printf("LCM of %d & %d = %d\n",a,b,lcm);
getch();
}
0 comments:
Post a Comment