Thread: Print Out Factors Of Numbers

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    3

    Print Out Factors Of Numbers

    i need to find the number of factors that are not greater than 1000 using a program built in C.

    I have got everythin workin ok, but when it comes to printf() i dont know what to put or how to get it to load the numbers to do what im asking for.

    everythin i have tried in printf isnt working, any ideas?

  2. #2
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    ok i'm gonna work on an assumption, (since you havent posted code) that your program looks a little like this:

    Code:
    int main ()
    {
            int a,b,c,d.....;
            float x,y,z.....; 
            char something;  /*and so on*/
    
            /*body of program*/
            .
            .
            .
    }
    here's how printf works:

    Code:
    printf ("Integer is %d, Float is %f, Character is %c\n", a, x, something);
    That should help you out, but you really should supply some code so we can see what you may have done wrong.
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Post your code.

  4. #4
    Registered User
    Join Date
    Jan 2006
    Posts
    3
    ive got at the top int isfactorof and int numfactors with code underneath each. then i have got void main():

    Code:
    void main()
    
    {	int i,  result, max; 
    
    while(i<1000) {result = numberfactor (i); 
    
    
    if (result > max) { max = result;} i++; 
    
    printf("Number of Factors are:\n\n number = %d\n\n", max);
    
    return;

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > void main()
    main returns an int

    > while(i<1000) {result = numberfactor (i);
    There's nothing here to increment i, and since you don't initialise i either, the loop either does nothing at all, or gets stuck forever.

  6. #6
    Registered User
    Join Date
    Jan 2006
    Posts
    3
    Quote Originally Posted by Salem
    > void main()
    main returns an int

    > while(i<1000) {result = numberfactor (i);
    There's nothing here to increment i, and since you don't initialise i either, the loop either does nothing at all, or gets stuck forever.
    this is where i get lost, and dont know what to do on it.

    what do i need to do so that i can fix this? what am i missing out?

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Braces and indentation mostly.
    Any consistent approach to formatting will make your code a lot easier to understand from the outset.
    Code:
    int main ( ) {
        int i, result, max;
        i = 0;  /* added initialisers */
        max = 0;
    
        while ( i < 1000 ) {
            result = numberfactor (i); 
            if (result > max) {
                max = result;
            }
            i++; 
        } /* probably missing, I couldn't tell */
    
        printf("Number of Factors are:\n\n number = %d\n\n", max);
    
        return 0;  /* successful run, return 0 */
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Adding Line numbers in Word
    By Mister C in forum A Brief History of Cprogramming.com
    Replies: 24
    Last Post: 06-24-2004, 08:45 PM
  2. Print numbers by inorder..
    By NightWalker in forum C Programming
    Replies: 5
    Last Post: 09-15-2003, 10:24 AM
  3. What kind of programs should I start writing?
    By Macabre in forum C++ Programming
    Replies: 23
    Last Post: 04-12-2003, 08:13 PM
  4. Replies: 1
    Last Post: 07-31-2002, 11:35 AM
  5. A (complex) question on numbers
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 02-03-2002, 06:38 PM