PDA

View Full Version : Some C Clarification

CLomax
December 30th, 2008, 11:40 PM
#include <stdio.h>
main()
{
int n,i=1,j,c;
printf("Number Of Terms Under 1000: \n\n");
n=1000;
while(i<=n)
{
c=0;
for(j=1;j<=i;j++)
{
if(i%j==0)
c++;
}
if(c==2)
printf("%d ",i);
i++;
}
}

For a university entrance scholarship I am likely to be asked to write an algorithm which produces the first 1000 prime numbers. This is the code I have written which seems to work fine (FYI, I gathered some ideas/snippets of code from parts of the internet).

The problem I'm having is that it isn't pseudo code. I can "translate" most of it but there are certain areas I'm having trouble with...

E.G
if(i%j==0)
What exactly is the red portion of the formula actually doing?

By the way, my indentation is far better than what the quote is showing :P.

samjh
December 30th, 2008, 11:49 PM
It's the modulo operator, which calculates the remainder of i divided by j.

Example:
If i = 5 and j = 2, then i % j = 1.
If i = 6 and j = 3, then i % j = 0.

CptPicard
December 31st, 2008, 12:16 AM
Did you actually write and understand it if you need to ask the question or did you just copy it from somewhere? :)

CLomax
December 31st, 2008, 12:43 AM
It's the modulo operator, which calculates the remainder of i divided by j.

Thank you very much, I understand perfectly :).

Did you actually write and understand it if you need to ask the question or did you just copy it from somewhere? :)

It was that one part which I did just copy (I will never do it again.) The rest I either wrote myself and got it right, or wrote myself but completely wrong and referred to other peoples programs which are somewhat similar.

It just happens that I took advantage of the fact that it's open source the wrong way. :)

samjh
December 31st, 2008, 01:30 AM
Just so you know, use the code tags to post code. It will preserve the indentation.

See: http://ubuntuforums.org/misc.php?do=bbcode#code