finding factors

This is a discussion on finding factors within the C Programming forums, part of the General Programming Boards category; i want to find factors of any given number. i am trying in the following way..how far is it right? ...

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    why do you use &#37;c format?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

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

    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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    If u r getting ur desired results then its right
    Works for me approach is not good enough
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    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, 09: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

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