1. ## 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

2. 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.

3. 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).

4. Originally Posted by MK27
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?

5. 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.)

6. Originally Posted by laserlight
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.

7. 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. 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?

9. 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
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. Crap do u no what is wrong wit the code?

11. Originally Posted by _Mike
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.

Originally Posted by Bubbles293
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.

12. 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.

13. for(i=2;i<=n;i++)
is it to do with this line

14. 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?

15. Originally Posted by Bubbles293
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.