# Thread: how do i calculate prime number

1. I believe both Manav and myself determined Pimp is not a good candidate to write a "Sieve" program.

My advice to Pimp was to use the simpler nested loops, and the mod operator for testing.

He needs to post up what he's trying now, so we can see what he's doing.

2. Which is the problem, do you lack the math and logic skills to understand how to algorithmically find a prime number, or are you just unsure how to program in C?
If it is the later, please provide full pseudocode for how you would do it, and we'll tell you what the correct syntax is to get from there to a working program.

3. i know the way to find prime number but i can't get it into C programming..

4. Pimp, show us what you're trying to make work.

It may help to just do one of the two nested loops, at a time. That is, for now just mark out the outer loop, and work on the inner loop, only.

/* while(blah blah blah) this is the outer loop, now marked out so the C compiler will ignore it */

for( blah blah; blah < blah; blah) /* this is the inner loop, and you can concentrate on it only */

5. Code:
```#include<conio.h>
#include<stdlib.h>
#include<stdio.h>

int main()
{
int numarray[100];// create space for array
int top,reminder,flag,value,prime;
int minusONE;//pointer for array

do
{
printf("pls enter top limit:");
scanf("%d",&top);
for(minusONE = top - 1; minusONE > 0; minusONE--)
{
reminder = top % minusONE;
printf("\n%d / %d reminder is %d", top, minusONE, reminder);
/* if(reminder==0)
{
top!=prime;

}
if(reminder!=0)
{
top==prime;
}
for(minusONE=top-1;minusONE;minusONE--)
{
printf("numarray[%02d]=%d\nn",top,numarray[top]);
}
_sleep(1000);

}*/

}while(minusONE >=3&&flag !=0);
{
minusONE--;
flag=value%minusONE;
}
system("pause");
return 0;

}```
this is the code that i did.. i only manage to get the remainder..
after which i have to store thse that rem!=0 into my array as prime number
then i have to print out those number and sum up.

6. OK. You have two nested loops, that's good. It's awkward working from the top, downward. For example, if the top number is 357, now we need to see how the code works.

So say we start working downward from 356, as your code does: 355 - not prime, 354 - not prime, 353 - not prime.

Eventually, it's going to hit you that you're doing a ........load of looping for nothing up here. You'll find *NO* primes for a number, above the square root of the number, see?

And do you know just off-hand if 357 can be divided evenly by say, 91? So it's horribly tedious to check your code, when you work from the top down.

Also, when remainder = 0, that number is NOT prime. Primes are numbers that have nothing that will divide into them evenly, except 1, of course.

7. ya know the part that if the rem is not 0 it will not be a prime but i can't get that part and save it into a array.. that the part i'm stuck and lost

8. ya. i know that part*

9. Originally Posted by pimp
Code:
```#include<conio.h>
#include<stdlib.h>
#include<stdio.h>

int main()
{
int numarray[100];// create space for array
int i, top, remainder, numberOfPrimes, prime;  Remainder, not reminder
int minusONE;//pointer for array

//Has to be moved out of the two nested loops for finding primes.
printf("pls enter top limit:");
scanf("&#37;d",&top);

prime = 1;
numberOfPrimes = 0;

do
{
for(minusONE = (top / 2); minusONE > 1; minusONE--) //Top / 2 is not the best, sqrt top is, but it's way better than top - 1
{
remainder = top % minusONE;
printf("\n%d / %d remainder is %d", top, minusONE, remainder);
if(remainder==0)  //it's not a prime number, so stop processing it.
{
prime = 0;
break;  //break out of the for loop.
}
} //end of for
// if prime == 1 at this point, you have a prime number. Add this if statement, and put the prime number, into the array.

if(blah blah blah)
{
array of primes[numberOfPrimes++] = top;

prime = 1;
}while(--top > 1);

//print up the arrray of primes
for(i = 0; i < numberOfPrimes; i++)
print array of primes[i];

system("pause");
return 0;

}```
this is the code that i did.. i only manage to get the remainder..
after which i have to store thse that rem!=0 into my array as prime number
then i have to print out those number and sum up.
I've made some changes, some highlighted, others are not, but easy to see.

Edit: OK, you want to save the primes into an array and print them out later.
I removed two loops near the bottom - no idea what they were about.

10. i saw some aid in there.. but i have a question. how do i stop processing if the remainder is =0.
i have the planning there but i some how can't get it into the program

11. you break out of the for loop (the inner loop, with a break statement, after setting prime = 0;

12. i don understand. can u type the code out ?basically i'm stuck at converting into C from my logic

13. Nope.

Look at post #24, it shows a lot of the logic you need, tucked into your program.

All untested, of course, but it's "close", and better than pseudo-code, which would do you little good.

14. Consider writing it as two functions

eg, in main() you have your prompts and this
Code:
```for ( p = 1 ; p < numPrimes ; p++ ) {
if ( isPrime(p) ) {
printf("&#37;d is prime\n", p );
}
}```
Then a function like this
Code:
```int isPrime ( int p ) {
int result = 0;
if ( p % 2 == 1 ) {
result = 1;
}
return result;
}```
Obviously it isn't correct, but it's enough to test that main() is actually prompting and looping correctly for example. When it is doing what you want, you're free to work on the detail of working out whether a given number is prime or not.

15. there is a part that i don't understand can explain more?

}while(--top > 3);

//print up the arrray of primes
for(i = 0; i < numberOfPrimes; i++)
print array of primes[i];

system("pause");
return 0;

}