# Prime Number Function

This is a discussion on Prime Number Function within the C Programming forums, part of the General Programming Boards category; Hey guys. I'm new to the forums and C, but I think I'm learning quickly. I think I got the ...

1. ## Prime Number Function

Hey guys. I'm new to the forums and C, but I think I'm learning quickly. I think I got the basic program down but I need to fine tune it for this next step.

For my main function I need to call isprime from 1 to 1000 using a for loop. If the call isprime(n) returns a 1, I need to print that number. After I've done this, I need to seperate them into columns after every tenth number. This I can't figure out.

Here's my code so far. Any hints would be appreciated. Thanks!

Code:
```#include "stdafx.h"

int isprime(int n);

int _tmain(int argc, _TCHAR* argv[])
{
int x, col = 0;

for ( x = 1; x <= 1001; x++)
{
if( isprime(x) == 1)
printf("%d  ", x);

}

}

int isprime(int n)
{
int div;

if(n == 2)
return 1;

for(div = 2; div < n; div++)
{
if(n % div == 0)
return 0;
}

return 1;

}```

2. your modified code
may be you want kind of this code

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

int isprime(int n);

#define EOL 10

int main()
{
int x, col = 0;
int print_count = 0;
for ( x = 1; x <= 1001; x++)
{
if( isprime(x) == 1) {
printf("%d\t", x);
++print_count;
if(print_count == 10) {
printf("\n");
print_count = 0;
}
}
}
printf("\n");
}

int isprime(int n)
{
int div;

if(n == 2)
return 1;

for(div = 2; div < n; div++)
{
if(n % div == 0)
return 0;
}

return 1;

}```

3. Perfect! I did some modifying since I had to use modulus but you pointed me in the right direction. Can you explain what #define EOL 10 is for?

Code:
```#include "stdafx.h"

int isprime(int n);

int main()
{
int x, count = 0;
for ( x = 2; x <= 1001; x++)
{
if( isprime(x) == 1) {
printf("%d\t", x);
++count;
if(count % 10 == 0) {
printf("\n");
count = 0;
}
}
}
printf("\n");
}

int isprime(int n)
{
int div;

if(n == 2)
return 1;

for(div = 2; div < n; div++)
{
if(n % div == 0)
return 0;
}

return 1;

}```

4. #define is a macro and it is a pre processor

means before compilation begins the compiler will replace the EOL with its value 10

simple did u understand or not

5. Got it. Thanks.

6. You are most Welcome Pal enjoy C/C++ programming

7. Originally Posted by RockyMarrone
#define is a macro and it is a pre processor

means before compilation begins the compiler will replace the EOL with its value 10
That still does not explain what EOL is for though

mrcg, although it helped you learn something new, in actual fact you should just remove that line since EOL is never used. Presumably RockyMarrone intended to use it as a named constant for the newline character, but here it is unnecessary since \n will do.

8. Yeah, I was wondering what EOL stood for, then it just popped in my head. End of Line. I know what pre processors do and was just trying to figure out why he inserted in. Nonetheless, I thanked him for the help. This forum is awesome! Looking forward to contribute in the near future!

9. Originally Posted by laserlight
That still does not explain what EOL is for though

mrcg, although it helped you learn something new, in actual fact you should just remove that line since EOL is never used. Presumably RockyMarrone intended to use it as a named constant for the newline character, but here it is unnecessary since \n will do.

No laser light my intent was not to use EOL as \n

if you read my code carefully i want to use it at

if (print_count == EOL)

but i forgot to replace 10 as EOL

10. Originally Posted by RockyMarrone
No laser light my intent was not to use EOL as \n

if you read my code carefully i want to use it at

if (print_count == EOL)

but i forgot to replace 10 as EOL
In that case perhaps PRIMES_PER_LINE would be a better name than EOL.

11. Ok you can do that

Popular pages Recent additions