Monday 16 March 2015

Program to find LCM and GCD of two numbers in C

 #include<stdido.h>
 void main()
 {
     int i,a,b,min,max,lcm,hcf;
     clrscr();
     printf("Enter the two numbers:");
     scanf("%d%d",&a,&b);
     //find out smallest and largest and assign them to min and max variable respectively
     a<b?(min=a,max=b):(min=b,max=a);
     printf("%d%d",min,max);
     for(i=min;i>=1;i--)
     {
if(max%i==0&&min%i==0)
{
hcf=i;
break;
}
     }
     lcm=(a*b)/hcf;
     printf("\nThe LCM=%d\nThe GCD=%d",lcm,hcf);
     getch();
 }

/*
Output:
Enter the two numbers:90
75                                                                            
7590                                                                          
The LCM=450                                                                  
The GCD=15                                                                    

*/
                                                                             
      

No comments:

Post a Comment