C language interview questions solution for freshers beginners placement tricky good pointers answers explanation operators data types arrays structures functions recursion preprocessors looping file handling strings switch case if else printf advance linux objective mcq faq online written test prime numbers Armstrong Fibonacci series factorial palindrome code programs examples on c++ tutorials and pdf
//LCM #include int main(){ int a,b,i=1,s; printf("Insert a: ");scanf("%d",&a); printf("Insert b: ");scanf("%d",&b); if (a==b){ printf("%d",a); } else if (a>b){ s=b; while (b%a!=0){ b=s*i; i++; if (b%a==0){ printf("LCM of three numbers is %d",b); } } } else{ s=a; while (a%b!=0){ a=s*i; i++; if (a%b==0){ printf("LCM of three number is %d",a); } } } }
24 comments:
#include
#include
voidmain()
{
inta,b,n,m,gcd,lcm;
clrscr();
printf("enter the values of a,b");
scanf("%d%d",&a,&b);
n=a;
m=b;
while(n>m)
{
if(n>m)
n=n-m;
else
m=m-n;
}
gcd=n;
lcm=a*b/gcd
printf("lcm of %d and %d is %d",a,b,lcm);
getch();
}
for loop se eassy hoga !!!!!!!
/*Best way to calculate an LCM*/
#include
main()
{
int a,b,i,max,min;
scanf("%d %d",&a,&b);
if(b>a)
{
max = b;
min = a;
}
else
{
max = a;
min = b;
}
for(i=1;;i++)
{
if((i*max)%min==0)
{
printf("LCM : %d\n",i*max);
break;
}
}
}
No,for loop doesn't make the program easy.
It only allows us to specify 3 things about a loop in single line.
Code can be made even smaller. Like this:
#include
main()
{
int a, b, i =1;
scanf("%d %d", &a, &b);
for(; a != b; i++)
{
if((a*i)%b == 0)
break;
}
printf("LCM is %d", a*i);
}
It is not necessary to find the max or min number.
You can directly proceed. You have to take care only when a equals to b.
its true....:)
ur program is not compiling with 2 and 5 or any other no. which have no common factors
What about with 3 numbers?
can any one tell, how to write code for LCM of 2 numbers without calculating GCD, using Transform-and-Conquer method ?
plz anybody tell program to find lcm of 3 numbers
This is the most efficient program___
#include
void main()
{
int a, b, i, x, y, gcd, lcm;
scanf("%d%d",&a,&b);
x=a;
y=b;
if(a==0)
gcd=a;
if(b==0)
gcd=b;
else{
while(b!=0){
i=b;
b=a%b;
a=i;
}
}
gcd=a;
lcm=(x*y)/gcd;
printf("%d",lcm);
}
// LCM of three numbers
#include
int main()
{
int num1, num2, num3, max;
printf("Enter three positive integers: ");
scanf("%d %d %d", &num1, &num2,&num3);
max=(num1>num2) ? num1 : num2;
max= (max>num3) ? max : num3;
while(1)
{
if(max%num1==0 && max%num2==0 && max%num3)
{
printf("LCM of %d, %d and %dis %d", num1, num2,num3,max);
break;
}
++max;
}
return 0;
}
For more information visit: www.techedinfo.com
its working
#include
int main ()
{
int t,i,j,a,b,h;
scanf("%d%d",&a,&b);
for(i=1,j=1 ;i<=a&&j<=b;i++,j++)
{
if(a%i==0 && b%j==0)
{
t=i;
}
}
printf("LCM- %d",t);
h=a*b/t;
printf("HCF- %d",h);
return 0;
}
if(max%num1==0 && max%num2==0 && max%num3) typing mistake in above statement
if(max%num1==0 && max%num2==0 && max%num3==0)
//LCM
#include
int main(){
int a,b,i=1,s;
printf("Insert a: ");scanf("%d",&a);
printf("Insert b: ");scanf("%d",&b);
if (a==b){
printf("%d",a);
}
else if (a>b){
s=b;
while (b%a!=0){
b=s*i;
i++;
if (b%a==0){
printf("LCM of three numbers is %d",b);
}
}
}
else{
s=a;
while (a%b!=0){
a=s*i;
i++;
if (a%b==0){
printf("LCM of three number is %d",a);
}
}
}
}
Anybody tell me plz Write a function LCM() that receives two integer arguments and returns LCM.
int main()
{
int a,b,c,i,m,l;
printf("enter a b c values:");
scanf("%d%d%d",&a,&b,&c);
/* if(a>b&&a>c)
l=a;
else if(b>c)
l=b;
else
l=c;*/
((a>b)&&(a>c))?l=a:b;
(b>c)?l=b:l=c;
for(i=1;i<=10000;i++)
{
m=l*i;
if(m%a==0&&m%b==0&&m%c==0)
{
printf("lcm of numbers is:%d",m);
break;
}
}
}
/*OUTPUT:-
enter a,b,c values :17 18 19
lcm of numbers:5814*/
int main()
{
int a,b,c,i,m,l;
printf("enter a b c values:");
scanf("%d%d%d",&a,&b,&c);
/* if(a>b&&a>c)
l=a;
else if(b>c)
l=b;
else
l=c;*/
((a>b)&&(a>c))?l=a:b;
(b>c)?l=b:l=c;
for(i=1;i<=10000;i++)
{
m=l*i;
if(m%a==0&&m%b==0&&m%c==0)
{
printf("lcm of numbers is:%d",m);
break;
}
}
}
/*OUTPUT:-
enter a,b,c values :17 18 19
lcm of numbers:5814*/
#include
#include
void main()
{
int x,y,a;
int lcm(int,int);
printf("enter two numbers:");
scanf("%d\t%d",&x,&y);
if(x>y)
{
a=lcm(x,y);
}
else
{
a=lcm(y,x);
}
printf("lcm of two numbers:%d",a);
getch();
}
int lcm(int x,int y)
{
int n=x;
while(1)
{
if(n%x==0&&n%y==0)
break;
n++;
}
return n;
}
Post a Comment