Thread: why the prime numbers cant be displayed?

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    2

    why the prime numbers cant be displayed?

    hi,i am using dev c++.
    here is my code.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    int is_prime(int n);
    
    void main()
    {
         int x;
         printf("How many prime numbers? ");
         scanf("%d", &x);
         printf("\n %d prime numbers: ",x);
         
         int j=1;
         int i=1;
         for(i=1;i<=x;i++)
         {
                 printf("\n %d :",i);
                 for(j=1;is_prime(j)==0;j++)
                    {
                                         if(is_prime(j)==1)
                                         printf("%d",j);
                                         }
                 
                 
         }
         
      system("PAUSE");	
      
    }
    //function definition
    int is_prime(int n)
    {
        int k, limit;
        if(n==1)
                return 0;
        if(n==2)
                return 1;
        if(n%2==0)
                  return 0;
        limit =n/2;
        for(k=3;k<=limit;k+=2)
                              if(n%k==0)
                                        return 0;
        return 1;
    }

  2. #2
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    well one thing to look at is for loop is never executed in your is_prime function

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    48
    Quote Originally Posted by rogster001 View Post
    well one thing to look at is for loop is never executed in your is_prime function
    No, it does.
    it does not print prime numbers because
    Code:
     for(j=1;is_prime(j)==0;j++)
    the test condition is wrong. 0 means it is NOT prime. So it does not get executed at all.If the inner for loop is for checking whether a number is prime or not fails, you still increment 'i' thinking that it has found a prime number, when in fact it hasn't. If j is always initialized to 1 in your j for loop and a condition in your function is_prime(n)
    Code:
    if(n==1)
                return 0;
    then it naturally fails every time from i to x.

    All you need is:
    Code:
    for(j=1; i <= x;j++)
    {
    		if(is_prime(j)==1) {
                                             printf("\n%d",j);
                                               i++;
    		}
    }

  4. #4
    Registered User
    Join Date
    Nov 2009
    Posts
    2
    thz, your explanation helps me much^^

  5. #5
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    BTW, you can speed up your program by changing:
    Code:
    limit =n/2;
    to:
    Code:
    limit = sqrt( n ) + 1;
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking very large prime numbers
    By password in forum C++ Programming
    Replies: 2
    Last Post: 02-11-2008, 12:26 PM
  2. Prime odd numbers
    By Lurker in forum C Programming
    Replies: 27
    Last Post: 03-24-2003, 04:09 PM
  3. prime numbers, counters, help!
    By vege^ in forum C++ Programming
    Replies: 1
    Last Post: 03-10-2003, 04:32 PM
  4. Help with prime numbers
    By Clean Killa in forum C++ Programming
    Replies: 5
    Last Post: 11-14-2002, 04:42 PM
  5. Prime Numbers
    By Korn1699 in forum C++ Programming
    Replies: 7
    Last Post: 11-03-2001, 09:52 PM