finding LCM

+1 chinesh doshi · November 20, 2015
#include<stdio.h>
int main()
{
    int a,b,i,lcm=1;
    printf("enter the 1 numbers \n");
    scanf("%d",&a);
    printf("enter the 2 numbers \n");
    scanf("%d",&b);
    for(i=2;i<=a;i++){
        if((a%i)==0&&(b%i)==0){
            a= a/i;
            b= b/i;
            lcm = lcm*i;
        }
        else if((a%i)==0&&(b%i)!=0){
            a =a/i;
            lcm = lcm*i;
        }
        else if((a%i)==0&&(b%i)!=0){
            b = b/i ;
            lcm = lcm*i;
        }
        else{
            a =a;
            b=b;
        }

    }
    printf("%d",lcm);
    return 0 ;
}

whats the problem here ??

Post a Reply

Replies

Oldest  Newest  Rating
0 chinesh doshi · December 25, 2015
no
0 Linguist Llama · December 27, 2015
"whats the problem here ?? "

I don't know. You haven't told us.

If your problem is calculating the lowest common multiple of two integers, then this code is functionally incompatible; it does too much. When a % i == 0 && b % i == 0 you have found the lowest common multiple. You should break out of the loop at this point... but instead your loop continues. Why? Do you hope to find an even lower multiple by incrementing i?

Why do you think you need to divide each number by each of its factors to find the lowest common multiple?
0 Vaggelis Dou · November 21, 2015
does it run anyways or not?
  • 1

C

107,225 followers
About

One of the most popular languages of all time.

Links
Moderators
Bucky Roberts Administrator