![]() |
| | #1 |
| Registered User Join Date: Feb 2010
Posts: 13
| #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 | |
| | #2 |
| mastering the obvious 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);
|
| MK27 is offline | |
| | #3 | |
| C++ Witch Join Date: Oct 2003 Location: Singapore
Posts: 11,296
| Quote:
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 | |
| | #4 | |
| Registered User Join Date: Jan 2010
Posts: 210
| Quote:
)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 | |
| | #5 | |
| C++ Witch Join Date: Oct 2003 Location: Singapore
Posts: 11,296
| Quote:
(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 | |
| | #6 | |
| Registered User Join Date: Jan 2010
Posts: 210
| Quote:
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 | |
| | #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 | |
| | #8 | |
| C++ Witch Join Date: Oct 2003 Location: Singapore
Posts: 11,296
| Quote:
__________________ 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 | |
| | #9 |
| Registered User Join Date: Jan 2010
Posts: 210
| |
| _Mike is online now | |
| | #10 |
| Registered User Join Date: Feb 2010
Posts: 13
| Crap do u no what is wrong wit the code? |
| Bubbles293 is offline | |
| | #11 | ||
| mastering the obvious Join Date: Jul 2008 Location: SE Queens
Posts: 5,131
| Quote:
Sorry about that.Quote:
![]() 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 11:11 AM. | ||
| MK27 is offline | |
| | #12 | |
| C++ Witch Join Date: Oct 2003 Location: Singapore
Posts: 11,296
| Quote:
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;
}
}
}
__________________ 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 | |
| | #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 | |
| | #14 | |
| C++ Witch Join Date: Oct 2003 Location: Singapore
Posts: 11,296
| Quote:
__________________ 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 | |
| | #15 |
| mastering the obvious Join Date: Jul 2008 Location: SE Queens
Posts: 5,131
| 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. |
| MK27 is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
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 |