Thread: finding factors

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    9

    finding factors

    i want to find factors of any given number. i am trying in the following way..how far is it right?

    Code:
    for(i=1; i<=num; i++)
    {
        if(num%i==0)
             printf("Factor = %c",i);
    }

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    why do you use &#37;c format?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Dec 2005
    Posts
    136

    Thumbs up

    Hey,
    If u r getting ur desired results then its right else wrong. what to worry about.
    S_ccess is waiting for u. Go Ahead, put u there.

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    If u r getting ur desired results then its right
    Works for me approach is not good enough
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    If you think about it, you only have to check for factors until sqrt(n). Beyond that point, any factors you find you already found (as long as you print them). Something like this:
    Code:
    for(x = 1; x*x < n; x ++) {
        if(n &#37; x == 0) {
            printf("Factor: %d\nFactor: %d\n", x, n/x);
        }
    }
    Just a suggestion.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Registered User
    Join Date
    Mar 2007
    Posts
    9
    Quote Originally Posted by javani View Post
    Code:
    for(i=1; i<=num; i++)
    {
        if(num%i==0)
             printf("Factor = %c",i);
    }
    sorry, there should be %d, not %c. But, thank you DWKS too for letting idea to minimize the iterations.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 02-19-2009, 10:32 PM
  2. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  3. Help with homework please
    By vleonte in forum C Programming
    Replies: 20
    Last Post: 10-13-2003, 11:16 AM
  4. MFC :: Finding Child Window of a CWnd* Object?
    By SyntaxBubble in forum Windows Programming
    Replies: 2
    Last Post: 09-06-2003, 09:06 AM
  5. Finding prime factors
    By ripper079 in forum C Programming
    Replies: 3
    Last Post: 05-17-2002, 09:23 PM