# Thread: determine if numbers are prime

1. ## determine if numbers are prime

i have to write a program that inputs a number and determines whether the number is prime. After displaying the results, ask the user if they want to try again. At the end, print Have a nice day!

The problem I´m having is that it only tells if a number is prime the first time, when i try it more times it just says not prime for any number, i would like to see if any of you can tell me what i´m doing wrong.

[
Code:
```#include <stdio.h>
#include <ctype.h>
int main()
{

int a,c=1,i,number;
do
{
{
printf("Enter number:");
scanf("%d", &number);
getchar();
for(i=1;i<=number;i++)
{
a=number%i;
if(a==0)
{
c=c+1;
}
}
if (c==3)

printf(" PRIME NUMBER\n");
else
printf(" NOT PRIME NUMBER\n");

}

printf("Do you want to continue?(y/n)");
getchar();
{
printf("Have a good day!\n"); break;
}
}
return 0;
}```

2. What is the value of c the second time it enters the do... while loop?

3. To determine if a number is prime, you have to check if it is divisable by 2 or any previous prime number. So all you have to do is store a list of the prime numbers you've found so far and check if anyone divides your value. The only problem with that approach is that you have to calculate those prime numbers every time you run the procedure.

EDIT: A solution is to precalcutate all prime numbers ( the ones that fit in an int ), store them in a static array and iterate that array untill you find the passed value. If that value isn't found, it isn't prime.
EDIT^2: If to the above solution you check in each iteration if the passed value is smaller that the current prime, you won't have to traverse the entire array to find it out.

4. Hello mr Royaro..
there is no need to write that much code.... simpli we can write like
// just am giving logic

main()
{
int i,x,n,y,c=0;
char ch;
// just am writing pf() instead of printf("") just edit it

lable: // when you press your choice y compiler comes here and strarts compilation from
// here
pf(enter no'r x);
for(i=1;i<=x;i++)
{
n=x%i;
if(n==0)
y++;
}
if(c==2)
pf(prime n'r);
else
pf(not pime);
pf(do u want to continue Y/N);
ch=getchar();
if(ch=='y')
goto lable;
else
printf("hav a nic.....");
getch();
}

i hope that it may helps you...!

6. Originally Posted by kmsreddy
Hello mr Royaro..
there is no need to write that much code.... simpli we can write like
1) Lose the goto crap. Use a do while loop instead... this isn't BASIC.
2) There is no reason to check every value when doing this function...
..... a) We already know 1 and 2 are prime numbers.
..... b) We already know that an even number cannot be prime.

Code:
```for( i = 3;i <= x; i += 2 )  // go twice as fast by checking only odd numbers.
{
n=x%i;
if(n==0)
y++;
}```

7. Hi,

Cheers !!

Code:
```#include <stdio.h>
#include <ctype.h>
int main()
{
float a=0;
int c=1,i,number;
do
{

printf("Enter number:");
scanf("%d", &number);
getchar();

mprime = 'Y';

for(i=1;i<=number;i++)
{
a=number%i;
if ((a==0) && ( i!=number) && (i!=1))
{

printf(" NOT PRIME NUMBER\n");
mprime = 'N';
break;
}
}
if (mprime =='Y')

printf(" PRIME NUMBER\n");

printf("Do you want to continue?(y/n)");
getchar();