Help with a program

This is a discussion on Help with a program within the C Programming forums, part of the General Programming Boards category; Hey guys! I just started out with C and thought the "Euler Project" problems would be a great way to ...

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    5

    Help with a program

    Hey guys!

    I just started out with C and thought the "Euler Project" problems would be a great way to practice.

    However, I'm stuck at Problem 5 (What is the smallest number that is evenly divisible by all of the numbers from 1 to 20?). A quick google confirmed that my method was correct but I apparently made a logical error somewhere that gives me 0 as the output always. Any help will be appreciated.
    Code:
    
    long lcm(long a, long b);
    long greater(long a, long b);
    long lesser(long a, long b);
    
    int main(void){
    
        long i;
    	long m = 1;
     
        for ( i = 1; i <= 20; i++ )
        {
            m = lcm( m, i);
        }
        
        printf("%lf\n", m);
    	system("PAUSE");
    	return 0;
    	 
    }
    
    long lcm(long a, long b)
    {
        long larger_num;
        long smaller_num;
        long answer;
        long gcd;
        
        larger_num = greater(a, b);
        smaller_num = lesser(a, b);
    
        while ( smaller_num != 0 )
        {
            gcd = larger_num - smaller_num;
            larger_num = greater (gcd, smaller_num);
            smaller_num = lesser (gcd, smaller_num);        
        }
    
        answer = a * b / larger_num;
        return answer;    
    }
    
    long greater(long a, long b)
    {
        if ( a > b)
        {
            return a;
        }
        else if ( b > a)
        {
            return b;
        }
        else
        {
            return a;
        }
    }
    
    long lesser(long a, long b)
    {
        if ( a < b)
        {
            return a;
        }
        else if ( b < a)
        {
            return b;
        }
        else
        {
            return a;
        }
    }

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I would bet that %lf is the printf() format for a float - in fact, a double.

    Use the right format for a long integer, of course.

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    5
    ahhh feel so dumb now.

    Thanks a ton!

    Edit: Still gives the wrong answer. Could anyone please point out the mistake I'm making?
    Last edited by frogfury; 05-10-2010 at 08:29 PM.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    If you put the print inside the loop, you will see that your program thinks that the LCM of 232,792,560 and 20 is 18,044,195, which is odd. (That is to say: your multiplications overflow, so you should rearrange your calculations to do a division first.)
    Last edited by tabstop; 05-10-2010 at 09:52 PM.

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    5
    Thank you!

    This warrants another question: is there a better IDE than DEV-C++ with regards to debugging?

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    http://www.codeblocks.org/ is the new IDE on top of gcc/mingw (which you're already using).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Registered User
    Join Date
    May 2010
    Posts
    5
    Thank you

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 12:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 12:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 09:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21