# 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; Why not just delete that line of code? It's unnecessary and a nuisance. I've dug up an old laptop I ...

1. Why not just delete that line of code? It's unnecessary and a nuisance.

I've dug up an old laptop I can try this on. If it will boot!

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

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

{
remainder = top % minusONE;
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 loop

if(prime==1)
{
arrayofprimes[numberofprimes++] = top;
}

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

for(i = 0; i < numberofprimes; i++)
{
printf("\nthe prime number in array[%02d]is %d ",i,arrayofprimes[i]);
}
printf("\n");

system("pause");
return 0;

}```
this is the code i have.. and it work.. can explain how those line work ?so i hav better view (=

3. The only tricky thing about it is the looping logic, once you know how to work with the mod operator.

It's a one to many relationship - you have a number, and you have to test it against a whole bunch of other numbers, (and that's the nested inside loop).

Then you finish that one number, and have to repeat it with another number. (that's the outer loop's job to keep feeding the inner loop with the next number to test).

If ANY test of mod on a number == 0, then the number isn't a prime, so you need that "prime" variable to flag and show the first time that happens, so the test on that one number can break off. That's the break statement in the inner for loop.

I'm off the board for most of the rest of today. You've still got the summation code to write up.

4. i'm done with the summation code.. anyway if possible can u copy my code and give explain it using the comment line on the program itself.. i wil be able to understand better(=

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

printf("pls enter top limit:");
scanf("&#37;d",&top);

prime = 1;
numberofprimes = 0;

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

for(minusONE; minusONE > 1; minusONE--)

{
remainder = top % minusONE;
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 loop

if(prime==1)
{
arrayofprimes[numberofprimes++] = top;
}

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

for(i = 0; i < numberofprimes; i++)
{
printf("\nthe prime number in array[%02d]is %d ",i,arrayofprimes[i]);
}
printf("\n");

system("pause");
return 0;

}```

5. That's not going to happen sorry. It's your code and your responsibility to know what it does. If you don't know how it works at the point you submit it, then ultimately that amounts to copying others work.
If you want the extra marks for commenting it, then you should spend the time to understand what you have, and comment it yourself.

If you really did already understand the maths and logic of how the program should work, and just had issues with the C syntax, then it should be pretty darn easy to figure out.

6. Well, if it wasn't NFL playoff weekend . . . nah, not really.

You need to step through the code and watch the variables as they change. Try to predict what they'll be on the next loop. Buddy on up to that code, a bit.

How would you describe the line of code? The obvious lines don't need a comment, But minusONE needs to be described better than "pointer to an array"! Both i and minusONE are loop control variables.

Do you need to include conio.h? What about stdlib.h? What's in these header files that your program uses?

And one last thing:

A statement in C always ends with a semi-colon. No exceptions.
Code:
```if(top < 5)
top = top/2;```
Needs no curly braces.

Code:
```for(i = 0; i < some_number; i++)
array[i] = (1/2 base * height);      //work with triangles now! :)```
And the for statement ends with the semi-colon also, and needs NO curly braces. Both the above are just ONE single statement.

ONLY when you want to have more than one statement as part of another statement (logically), do you need the curly braces.
Code:
```if(top < 5)
{
top = top/2;               //first statement
area = 1/2 base * height;  //second statement
}```
Here, the if statement needs the curly braces, because it encloses two or more statements, together.

Pimp, I've gone beyond the normal boundaries of being helpful, because I thought you really needed the extra help. It's way too time consuming to this again, however.

Page 4 of 4 First 1234
Popular pages Recent additions