TASK-
Write a C-program to determine LCM or HCF of two integer values. Your code should
first of all provide two choices 1 or 2 to the user to opt for LCM or HCF. Take the choice,
then ask the user to enter two integer values. Your code should then display the answer as
per given choice.
LCM: Least common multiple , its a number which is closer to certain with having quality of being least common multiple for certain numbers.
E.g LCM of 2 and 6 is 6, The common multiple of 2,6 also include 12 but least one is 6.
HCF= The highest common factors of each member of group
CODE:
#include<stdio.h>
#include<stdlib.h>
void lcm(int n,int m); //Function For Lcm
void hcf(int n,int m); //Function For Hcf
int main(){
int choose,n1,n2;
printf("1- LCM\n2- HCF\nplease choose: "); //Giving options
scanf("%d",&choose);
printf("\nEnter two numbers: "); //Taking two numbers from user
printf("\n1- ");
scanf("%d",&n1);
printf("\n2- ");
scanf("%d",&n2);
switch (choose)
{
case 1:
lcm(n1,n2); //Passing Arguments to the function
break;
case 2:
hcf(n1,n2); //Passing Arguments to the function
break;
default:
exit(0);
}
getchar();
return 0;
}
// Body of Lcm function
void lcm(int n,int m)
{
int i,Lcm=1,f=n*m;
if (n<m)
i=n;
else
i=m;
for (i;i<=f;i++)
{
if (i%n==0&&i%m==0)
{
Lcm=i;
break;
}
}
printf("Lcm of %d and %d is %d",n,m,Lcm);
}
//Body Of Hcf Function
void hcf(int n,int m)
{
int h,i,HCF=1;
for (i=1;i<=(n*m);i++)
{
if (n%i==0&&m%i==0)
HCF=i;
}
printf("Hcf of %d and %d is %d",n,m,HCF);
}
Expected Output (LCM):
Expected Output (HCF):
Precious material that is giving to us.
ReplyDelete