distinct positive factors

This is a discussion on distinct positive factors within the C Programming forums, part of the General Programming Boards category; #include<stdio.h> #include<math.h> int main(void) { int n, i, a; printf("enter the number"); scanf("%d", &n); for (i = 2; i <=n; ...

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    13

    Question distinct positive factors

    #include<stdio.h>
    #include<math.h>

    int main(void)
    {
    int n, i, a;
    printf("enter the number");
    scanf("%d", &n);
    for (i = 2; i <=n; i++)
    {
    a = n % i;
    if (a == 0)
    {
    printf("the number is not a prime number");
    return 1;
    }
    else
    {
    printf("the number is prime");
    return 0;
    }
    }
    }
    My code has a problem
    Last edited by Bubbles293; 02-08-2010 at 08:30 AM.

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    A prime number is a number that cannot be evenly divided by any whole number.

    Here's a hint: the simplest method for doing this involves modulus:
    Code:
    for (i=0; i<num; i++) 
        if (!(num%i)) printf("%d is not prime.",num);
    ! is negation, ie, "not true", aka FALSE. False in C == 0.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,630
    Quote Originally Posted by Bubbles293
    the code i have so far is
    I suggest that you indent your code properly and test it. At the moment, it does not do what you probably expect, even if it has a correct idea.

    Now, for counting the number of distinct factors: are you able to identify the factors in the first place? This is similiar to what you did to find if a number has a factor, or no factors at all (i.e., the part on primality testing).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Jan 2010
    Posts
    412
    Quote Originally Posted by MK27 View Post
    A prime number is a number that cannot be evenly divided by any whole number.
    By any whole number greater than 1. All numbers can be evenly divided by 1 so your code would claim all numbers as non-prime (if you add exception handling for the divide by zero exception )

    Edit: btw, does C have built-in exception handling or would you have to use APIs for that?
    Last edited by _Mike; 02-07-2010 at 09:36 AM.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,630
    Quote Originally Posted by _Mike
    By any whole number greater than 1.
    Considering the code posted, I think that Bubbles293 understands what is a prime number, so there is no need to elaborate, especially not when you make a correction that is still incorrect (2 is evenly divisible by 2, hence 2 is not a prime number, by your improved definition.)
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Jan 2010
    Posts
    412
    Quote Originally Posted by laserlight View Post
    Considering the code posted, I think that Bubbles293 understands what is a prime number, so there is no need to elaborate, especially not when you make a correction that is still incorrect (2 is evenly divisible by 2, hence 2 is not a prime number, by your improved definition.)
    Ah yes, sorry. I meant to write ".. greater than 1, except itself"
    And I didn't mean to imply that Bubbles293 doesn't know what a prime is, if it came out that way I applogize. It was more meant for random person X who finds this thread and tries MK27's code and can't understand why it doesn't work properly.
    Last edited by _Mike; 02-07-2010 at 09:58 AM. Reason: typo

  7. #7
    Registered User
    Join Date
    Feb 2010
    Posts
    13
    Hi i ran the code again and it does the is it prime or is it not why do you think it does not do what i think it does i am confused

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,630
    Quote Originally Posted by Bubbles293
    Hi i ran the code again and it does the is it prime or is it not why do you think it does not do what i think it does i am confused
    Test with the number 9 as input. Is 9 a prime number? Does your program report that 9 is a prime number?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Jan 2010
    Posts
    412
    Quote Originally Posted by Bubbles293 View Post
    Hi i ran the code again and it does the is it prime or is it not why do you think it does not do what i think it does i am confused
    If you indent the code properly it would be easier to see that any number you input is only ever tested against "number mod 2"

  10. #10
    Registered User
    Join Date
    Feb 2010
    Posts
    13
    Crap do u no what is wrong wit the code?

  11. #11
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by _Mike View Post
    By any whole number greater than 1. All numbers can be evenly divided by 1 so your code would claim all numbers as non-prime (if you add exception handling for the divide by zero exception )
    Whoops. Well it's the idea that counts Sorry about that.

    Edit: btw, does C have built-in exception handling
    Yes, it's called the Operating System.



    Quote Originally Posted by Bubbles293 View Post
    Crap do u no what is wrong wit the code?
    Hmm, well, what does it do that you don't like? If you mean the code I posted earlier, it's just that the first "for" parameter should be i=2, not i=0.
    Last edited by MK27; 02-07-2010 at 10:11 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,630
    Quote Originally Posted by Bubbles293
    Crap do u no what is wrong wit the code?
    Yes. Firstly, it is very poorly formatted. Here is an example of your code with good formatting:
    Code:
    #include<stdio.h>
    #include<math.h>
    
    int main(void)
    {
        int n, i, a;
        printf("enter the number");
        scanf("%d", &n);
        for (i = 2; i <=n; i++)
        {
            a = n % i;
            if (a == 0)
            {
                printf("the number is not a prime number");
                return 1;
            }
            else
            {
                printf("the number is prime");
                return 0;
            }
        }
    }
    _Mike has described the logic error that my test input demonstrates.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    Registered User
    Join Date
    Feb 2010
    Posts
    13
    for(i=2;i<=n;i++)
    is it to do with this line

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,630
    Quote Originally Posted by Bubbles293
    for(i=2;i<=n;i++)
    is it to do with this line
    Not really, but in a small way: how many times does that loop loop?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #15
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Bubbles293 View Post
    for(i=2;i<=n;i++)
    is it to do with this line
    i<=n should be i<n, since n%n will always have a remainder of 0.

    If you go this route you need to first check for 0 and 1, which are special cases.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Page 1 of 2 12 LastLast
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. how to handle integer overflow in C
    By kate1234 in forum C Programming
    Replies: 8
    Last Post: 04-23-2003, 12:20 PM

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