Thread: Please help w/ prime # program Almost done...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    9

    Question 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. #2
    julie lexx... btq's Avatar
    Join Date
    Jun 2002
    Posts
    161
    ..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
    ...viewlexx - julie lexx

  3. #3
    Green Member Cshot's Avatar
    Join Date
    Jun 2002
    Posts
    892
    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;
    Try not.
    Do or do not.
    There is no try.

    - Master Yoda

  4. #4
    Registered User
    Join Date
    Oct 2002
    Posts
    9
    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. #5
    Green Member Cshot's Avatar
    Join Date
    Jun 2002
    Posts
    892
    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.
    Try not.
    Do or do not.
    There is no try.

    - Master Yoda

  6. #6
    Registered User
    Join Date
    Oct 2002
    Posts
    9
    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. #7
    Green Member Cshot's Avatar
    Join Date
    Jun 2002
    Posts
    892
    You need to put return 1 outside the for loop.
    Try not.
    Do or do not.
    There is no try.

    - Master Yoda

  8. #8
    Registered User
    Join Date
    Oct 2002
    Posts
    9

    Talking

    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. #9
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    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
    hello, internet!

  10. #10
    Green Member Cshot's Avatar
    Join Date
    Jun 2002
    Posts
    892
    That's right. Forgot my math rules
    Try not.
    Do or do not.
    There is no try.

    - Master Yoda

  11. #11
    Registered User
    Join Date
    Oct 2002
    Posts
    26

    Thumbs up 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');





    }
    C++ Is cool

  12. #12
    [STARES/]

    read the announcement about [CODE] tags at the top of all forums.
    Compilers:
    GCC on Red Hat 8.1 (Primary)
    GCC on Mac OS X 10.2.4 (Secondary)

    Others:
    MinGW on XP

  13. #13
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    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
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. prime factorisation program
    By szpengchao in forum C Programming
    Replies: 4
    Last Post: 04-07-2009, 04:30 PM
  2. Date program starts DOS's date
    By jrahhali in forum C++ Programming
    Replies: 1
    Last Post: 11-24-2003, 05:23 PM
  3. C Program for Prime Numbers
    By mmuhlenb in forum C Programming
    Replies: 12
    Last Post: 02-19-2003, 04:55 AM
  4. problem with my prime number program
    By datainjector in forum C Programming
    Replies: 4
    Last Post: 07-12-2002, 12:30 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM