You are using a version without Ads of this website. Please, consider donating:

### Prime number

We have to write a loop statement to find the absolute value of a number and then find out if it is prime or not. I am not sure what I am doing wrong but it only prints out that everything is not a prime number. Here is part of the code that I am working on

 ``12345678910111213141516`` ``````int prime=1; int x; case 4: printf("Enter a whole number:"); scanf("%d",&prime); {prime=abs(prime);} for (x = 1; x <= prime; x++) if (prime%x==0) if (prime>1) printf("%d is not a prime number\n", prime); else printf("%d is prime\n", prime); break;``````
If you divide any number from 1 you always will get true in if (prime%x==0)
Also, the loop includes prime in its bounds, so as well as checking (prime % 1 == 0), it's checking (prime % prime == 0), which are always true for primes. Your loop only needs to go halfway to prime, by the way, since the smallest factor other than 1 that a composite number could have is 2.
Topic archived. No new replies allowed.