# how do i calculate prime number

This is a discussion on how do i calculate prime number within the C Programming forums, part of the General Programming Boards category; Originally Posted by pimp there is a part that i don't understand can explain more? }while(--top > 3); //print up ...

1. Originally Posted by pimp
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;

}
The variable top needs to be decremented by 1, every time the program loops through the outer do while loop.

I had it stop at 4, because I already know lower (positive integers) are prime. If you want 2 and 3 in your prime number array, just change the 3 to 1.

The print out of the prime numbers array should be straight forward to understand, yes?

You'll need to add the code to "sum" up the array numbers, whatever you're supposed to do with that.

2. i show u the code that i have edit.it about the same as yours..u try and run it..

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

int main()
{
int arrayofprimes[100];// create space for array
int top,remainder,numberofprimes,value,prime,i;
int minusONE;//pointer for array

//Has to be moved out of the two nested loops for finding primes.
printf("pls enter top limit:");
scanf("%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(remainder==1)
{
arrayofprimes[numberofprimes++] = top;
}

}while(--top > 3);

//print up the arrray of primes
for(i = 0; i < numberofprimes; i++)//why did u declare i ? what is the function of i ?
{
printf("array[%02d]",arrayofprimes[i]);
}

system("pause");
return 0;

}```
i'm really a C program idiot.

3. i manage to get prime number.. even though i change the 3 to 1 i still can't count number smaller than 4..

4. The variable i is used by convention, as a loop counter, by programmers.

I can't try the code, Pimp. My computer is involved in a very complex protein modeling simulation in Linux, and my compilers are all in Windows.

You're not going to want the word "array[] printed out before every single prime, are you?

Just print one line like "This is the primes that were found: " one time, before the for loop to print out all the actual prime numbers.

Good idea to make them all line up, I like that!

5. adak i show u the code that i was expected to do so u will have a idea about my project.which i can actually ask u for more help(=

summation of prime number
===================

the prime number in primearray[00] is 7
the prime number in primearray[01] is 5
the prime number in primearray[02] is 3
the prime number in primearray[03] is 2
the prime number in primearray[04] is 1

the sum of all prime number within the top limit is:
1+2+3+5+7=18

this is the actual output that i need to summit.
another question.. will i be able to change the i variable to other variable?

6. Replace this:
Code:
`       for(minusONE = (top / 2); minusONE > 1; minusONE--) //Top / 2 is not the best, sqrt top is, but it's way better than top - 1`
with this:
Code:
```  if(top > 5)
minusONE = top / 2;
else
minusONE = top - 1;

for(; minusONE > 1; minusONE--) //note the two semi-colons!

prime = 1;

just before the while(--top > 1) line.```
Edit: Note that there is no primearray[00], primearray[01], etc. There is just primearray[0], primearray[1], etc.

You can call the i variable anything you want, but get used to using it and seeing it - everybody uses it. It's as much a part of C
(and other programming languages), as logic, itself. If you needed a second loop variable of no particular name, j would be used,
as well.

7. can't i use &#37;02d to output a double digit out ? just a question.. i go try the code out and update .

8. Yes, but you can't use a format number for printing, as an index number in an array!

9. i'm back. i try the code and it is able to give me prime number..however it was at the opp side

array[07] is 1
array[05] is 2
array[03] is 3
array[02] is 4
array[01] is 5 // this is the code that i printe out

but the code that i need to print is

array[00] is 7
array[01] is 5
array[02] is 3
array[03] is 2
array[04] is 1

sorry for asking so much. i'm really sux in C program

10. Replace: If(remainder == 1) with: if(prime == 1).

We don't care WHAT the remainder is. Also, the variable "value" is never used and can be deleted.

11. can't work they print out the same output

12. Please post up the whole current program, so I can see just where we're at.

printf("array[&#37;02d]",arrayofprimes[i]);

with this:
printf("\n array[%2d]: %2d", i, arrayofprimes[i]);

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

int main()
{
int arrayofprimes[100];// create space for array
int top,remainder,numberofprimes,prime,i;
int minusONE;//pointer for array

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

prime = 1;
numberofprimes = 0;

do
{
if(top > 5)
{
minusONE = top / 2;
}
else
{
minusONE = top - 1;
}

for(minusONE; minusONE > 1; minusONE--) //note the two semi-colons!

// 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(prime==1)
{
arrayofprimes[numberofprimes++] = top;
}

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

//print up the arrray of primes
for(i = 0; i < numberofprimes; i++)//why did u declare i ? what is the function of i ?
{
printf("\narray[%02d]is %d ",arrayofprimes[i],i);
}
printf("\n");

system("pause");
return 0;

}```

14. Swap positions of i and arrayofprimes[i], in the next to last print statement.

like this: printf("\n array[&#37;2d]: %2d", i, arrayofprimes[i]);

How's the printout?

15. the print out is the same .. however it still print out the remainder which i don want

Page 3 of 4 First 1234 Last