This is a discussion on Please help w/ prime # program Almost done... within the C Programming forums, part of the General Programming Boards category; My son & I have been working on this program for hours....days....forever. Output should be all primes from 1-100, but ...

1. ## Please help w/ prime # program Almost done...

My son & I have been working on this program for hours....days....forever. Output should be all primes from 1-100, but is printing even numbers instead. First time separating function from main, and trying to do it with boolean and looping has thrown us for a loop - we've generated at least 100+ tries. Any hints or pointers would be very much appreciated!

Code:
```#include<stdio.h>
#include<math.h>
#include"simpio.h"
#include"genlib.h"

#define LowerLimit 2
#define UpperLimit 100

#define TRUE 1
#define FALSE 0

bool isPrime (int num);

main()

{

int num;

printf("The prime numbers in the range of 1-100 are:\n\n");

for (num=LowerLimit; num<=UpperLimit; num++)

if (isPrime (num) == 1) printf("%d\n",num);

}

bool isPrime (int num)

{

int j;

for (j=2; j<=sqrt(num); j++)

{
if (num%j==0) return 1;
else return 0 ;
}

}```

2. ..try something like:
Code:
```#include<stdio.h>
#include<math.h>
#include"simpio.h"
#include"genlib.h"

#define LowerLimit 2
#define UpperLimit 100

#define TRUE 1
#define FALSE 0

bool isPrime (int num);

main()

{

int num;

printf("The prime numbers in the range of 1-100 are:\n\n");

for (num=LowerLimit; num<=UpperLimit; num++)

if (isPrime (num)) printf("%d\n",num);

}

bool isPrime (int num)

{

int j;

for (j=2; j<=sqrt(num); j++)
{
if (num%j == 0){ return 0;};
}
return 1 ;

}```
made the changes bold..
hope this helps

/btq

3. 2 key mistakes
1) You want to return 1 if it is a prime number, not if it isn't
2) You need to return 0 outside the for loop because it is prime only if it has failed dividing evenly into 2 all the way to sqrt(num).

Code:
```for (j=2; j<=sqrt(num); j++)
{
if (num%j==0)
return 0;
}
return 1;```

4. Thanks to both of you - but now with your suggested changes have opposite problem, program is generating all odd # now instead of primes

There has to be something else that we're doing wrong - sorry to have been so dumb about the returns, we've moved every piece of that code around so much that we are certainly making stupid mistakes at this point.

5. bool isPrime (int num); (no bool types in c)
change to
int isPrime(int num);
Also change it during your function declaration

Now all you need to do is modify it to print 1 since that's a prime number also.

6. Sigh. We've made the suggested modifications, but the program will only generate all odd numbers from 1-99.....

Code:
```#include<stdio.h>
#include<math.h>
#include"simpio.h"
#include"genlib.h"

#define LowerLimit 1
#define UpperLimit 100

#define TRUE 1
#define FALSE 0

int isPrime (int num);

main()

{

int num;

printf("The prime numbers in the range of 1-100 are:\n\n");

for (num=LowerLimit; num<=UpperLimit; num++)

if (isPrime (num) == 1) printf("%d\n",num);

}

int isPrime (int num)

{

int j;

for (j=2; j<=sqrt(num); j++)

{
if (num%j==0)
{
return 0;
}
return 1 ;
}

}```

7. You need to put return 1 outside the for loop.

8. Thank you, thank you, thank you!!!

It would've taken over a week to have worked that out with my son's long distance learning tutor via email, we work in such a vacuum here that it can be very frustrating at times.

We really appreciate your help!

9. Originally posted by Cshot
bool isPrime (int num); (no bool types in c)
change to
int isPrime(int num);
Also change it during your function declaration

Now all you need to do is modify it to print 1 since that's a prime number also.
1 is not prime

10. That's right. Forgot my math rules

11. ## Hi this will help solve your problem

Hi i see that you trying to find the first 100 number that are prime what this code does is that it tells the user the first 100 prime numbers and then it asks the user if it wont's to continul will press y for yes and n for no if user enters (y)then the computer will tell the next 100 prime numbers but if the user enters n the program should end ok ..............Work more on it C trust me u will love it more and more
Here is the code make sure you go overe all the lines to see what's going on ok

#include <stdio.h>
void main()
{
int num=1,i,counter=1,flag, result=0;

do {
while(counter%101!=0)
{

for (i=2, flag=1; (i<=(num/2)) && flag
{
if ((num % i ) ==0)

flag =0;
else
i++;
}
if (flag==1)
{
printf("%d is prime\n", num);
counter++;

}
num++;
}
printf ("Press y to continue,\n ");
printf("press n and enter to stop:");
scanf("%s", &result);
counter++;

}
while(result=='y');

}

12. [STARES/]

read the announcement about [CODE] tags at the top of all forums.

13. I really don't think we needed to drag up a 6 day old thread to complain about lack of code tags.

Especially when there's a void main() in there

Popular pages Recent additions