Thread: How do I do a program with factors?

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    38

    How do I do a program with factors?

    Im doing a program that has to ask the user for two positive integers and outputs. I need it to have the inputs, all the factors of each (include 1 and itself), the prime factorization of each, the least common multiple (LCM) of the two integers, and the greatest common factor (GCF) of the two integers. I already started but Im having problems starting the factors. I have no idea how to do it. Do I have to go through each and every single number to check the input numbers or what? Im totally confused. I hope theres someone here who can help me out with this little problem. Thanks

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    void main(void)
    
    {
       int num1;
       int num2;
       
       printf("Please enter two positive numbers: \n");
       scanf("%d%d%", &num1, &num2);     
    
       if ((num1 < 0) || (num2 < 0))
           {
            printf("\n\nPlease enter all positive values!\n");
           }
       else
           { 
    
    }
    }

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    162
    You don't nessicarily need the numbers to be positive. In order to do the prime factorization of a negative number is to factor in a
    -1. I am not sure how to do all those other things though, but to find the GCF of the two numbers, you have to get the prime factorization and then find matching terms in different numbers and multiply different matching terms. EX:

    30: 1 * 2 * 3 * 5
    12: 1 * 2 * 2 * 3

    One pair of three's and two's match to you multiply three and two and you get 6 the GCF of 30 and 12. Hope this helps you!

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    Here is *the algorithm* for finding GCF of 2 numbers...

    Code:
    int GCF (int i1, int i2)
    {
     int i3;
     for (;;)
     {
      i3 = difference between i1 and i2;
      if (i3 == 0) break;
      bigger of i1 and i2 = i3;
     }
     return i1;
    }
    Let's run through this code...
    Code:
    i1: 27 27 18  9
    i2: 36  9  9  9
    i3:  9 18  9  0  ...  return 9;
    Code:
    i1: 96 69 42 15 15  3  3  3  3
    i2: 27 27 27 27 12 12  9  6  3
    i3: 69 42 15 12  3  9  6  3  0  ... return 3;
    There are quicker ways to check when to terminate the loop, but this one does work.

    Once you have the GCF of two numbers, finding the LCM is pretty simple...
    LCM (a, b) = a * b / GCF (a, b)
    Callou collei we'll code the way
    Of prime numbers and pings!

  4. #4
    Unregistered
    Guest

    Wink

    You mean a ; ), not a , don't you?

    (Check the "disable smilies" box or put a space between the ';' and the ')')

    Just thought I'd point this out

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  2. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  3. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 11:17 PM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM