C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 02-07-2010, 10:12 AM   #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 09:30 AM.
Bubbles293 is offline   Reply With Quote
Old 02-07-2010, 10:21 AM   #2
mastering the obvious
 
MK27's Avatar
 
Join Date: Jul 2008
Location: SE Queens
Posts: 5,131
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.
__________________

"A man can't just sit around." -- Larry Walters
MK27 is offline   Reply With Quote
Old 02-07-2010, 10:24 AM   #3
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 11,296
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).
__________________
C + C++ Compiler: MinGW port of GCC
Build + Version Control System: SCons + Bazaar

Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
laserlight is offline   Reply With Quote
Old 02-07-2010, 10:34 AM   #4
Registered User
 
Join Date: Jan 2010
Posts: 210
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 10:36 AM.
_Mike is online now   Reply With Quote
Old 02-07-2010, 10:43 AM   #5
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 11,296
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.)
__________________
C + C++ Compiler: MinGW port of GCC
Build + Version Control System: SCons + Bazaar

Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
laserlight is offline   Reply With Quote
Old 02-07-2010, 10:56 AM   #6
Registered User
 
Join Date: Jan 2010
Posts: 210
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 10:58 AM. Reason: typo
_Mike is online now   Reply With Quote
Old 02-07-2010, 10:59 AM   #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
Bubbles293 is offline   Reply With Quote
Old 02-07-2010, 11:04 AM   #8
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 11,296
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?
__________________
C + C++ Compiler: MinGW port of GCC
Build + Version Control System: SCons + Bazaar

Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
laserlight is offline   Reply With Quote
Old 02-07-2010, 11:05 AM   #9
Registered User
 
Join Date: Jan 2010
Posts: 210
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"
_Mike is online now   Reply With Quote
Old 02-07-2010, 11:07 AM   #10
Registered User
 
Join Date: Feb 2010
Posts: 13
Crap do u no what is wrong wit the code?
Bubbles293 is offline   Reply With Quote
Old 02-07-2010, 11:08 AM   #11
mastering the obvious
 
MK27's Avatar
 
Join Date: Jul 2008
Location: SE Queens
Posts: 5,131
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.

Quote:
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.
__________________

"A man can't just sit around." -- Larry Walters

Last edited by MK27; 02-07-2010 at 11:11 AM.
MK27 is offline   Reply With Quote
Old 02-07-2010, 11:09 AM   #12
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 11,296
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.
__________________
C + C++ Compiler: MinGW port of GCC
Build + Version Control System: SCons + Bazaar

Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
laserlight is offline   Reply With Quote
Old 02-07-2010, 11:10 AM   #13
Registered User
 
Join Date: Feb 2010
Posts: 13
for(i=2;i<=n;i++)
is it to do with this line
Bubbles293 is offline   Reply With Quote
Old 02-07-2010, 11:12 AM   #14
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 11,296
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?
__________________
C + C++ Compiler: MinGW port of GCC
Build + Version Control System: SCons + Bazaar

Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
laserlight is offline   Reply With Quote
Old 02-07-2010, 11:13 AM   #15
mastering the obvious
 
MK27's Avatar
 
Join Date: Jul 2008
Location: SE Queens
Posts: 5,131
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.
__________________

"A man can't just sit around." -- Larry Walters
MK27 is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
How do I print out all prime factors in C and all the distinct prime factors of the i Powervele C Programming 3 02-19-2009 10:32 PM
Binary Search Trees Part III Prelude A Brief History of Cprogramming.com 16 10-02-2004 03:00 PM
Help with homework please vleonte C Programming 20 10-13-2003 11:16 AM
how to handle integer overflow in C kate1234 C Programming 8 04-23-2003 12:20 PM


All times are GMT -6. The time now is 12:42 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

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