Examine this program? Tell me its bugs ?

Discussion in 'C' started by lionaneesh, Mar 28, 2010.

  1. lionaneesh

    lionaneesh Active Member

    Joined:
    Mar 21, 2010
    Messages:
    848
    Likes Received:
    224
    Trophy Points:
    43
    Occupation:
    Student
    Location:
    India
    Code:
    #include<stdio.h>
    
    int lcm(int a,int b); 
    
    int main()
    {
        int a;
        int b;
    
        printf("Enter two numbers to find lcm of ? :-\n1. ");
        scanf("%d",&a);
        printf("2. ");
        scanf("%d",&b);
        printf("%d is the lcm of %d and %d\n\n",lcm(a,b),a,b);
        getchar();
        return(0);
    }
    
    /******************* FUNCTION *****************************/
    
    int lcm(int a,int b)
    {
        int i,g,pr,z,q,dummy,lc = 0;
    
    /************************************************************************/
        
    /********* Determining weather a is a prime or not **************/
        
        
        for(i = 2 ;i < a;i++)      /* The loop */
        {
            pr = (a % i);
    
            if(pr == 0)
            {    
                dummy=14296;
                break;
            }    
        }
        
        if(dummy != 14296)
        {
            lc = a * b;
        }
    
    dummy = 0;
        
    /********* Determining weather a is a prime or not **************/
    
    
        for(i = 2 ;i < b;i++)      /* The loop */
        {
            pr = (b % i);
    
            if(pr == 0)
            {    
                dummy=14296;
                break;
            }    
        }
        
        if(dummy != 14296)
        {
            lc = a * b;
        }
    /************************************************************************/
        if((a % b) == 0)
        {
            lc = b/a;
        }
        if((b % a) == 0)
        {
            lc = a/b;
        }
    /************************************************************************/
        
        if(b>a)
        {
            g = b;
        }
        else
        {
            g = a;
        }
            
        for(z=1;z <= g;z++)
        {
            if(((b % z) == 0) && ((a % z) == 0))
            {
                lc = (b/z) * (a/z);
            }
        }
    /********************************************************************/
            return(lc);
    }
    This is a lcm finding program.
    please tell me if there are any bugs in this program and tell me how u liked my idea.
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    Its just too perfect. Don't you think the same.
     
  3. lionaneesh

    lionaneesh Active Member

    Joined:
    Mar 21, 2010
    Messages:
    848
    Likes Received:
    224
    Trophy Points:
    43
    Occupation:
    Student
    Location:
    India
    REly.......

    thanxxxxxxxxxx...........
     
  4. en_7123

    en_7123 New Member

    Joined:
    Feb 11, 2010
    Messages:
    105
    Likes Received:
    0
    Trophy Points:
    0
    The code is fine but you can write a function for checking if the number is prime or not that would make your code smaller.
     
  5. lionaneesh

    lionaneesh Active Member

    Joined:
    Mar 21, 2010
    Messages:
    848
    Likes Received:
    224
    Trophy Points:
    43
    Occupation:
    Student
    Location:
    India
    kk sir thanx and will look forward abt it while writing the code next time.

    thanx..:charming:
     
  6. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    Better to set a flag to 1 or 0 than to use some mystical value 14296 - reviewers will be thinking "what's the significance of 14296?"

    It would be better to use meaningful variable names than single letters. Suppose I give you the formula a=b*c^d - could you tell me what that means?
     
  7. lionaneesh

    lionaneesh Active Member

    Joined:
    Mar 21, 2010
    Messages:
    848
    Likes Received:
    224
    Trophy Points:
    43
    Occupation:
    Student
    Location:
    India
    Thank u sir fr ur advice ...
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice