Thread: help in find prime numbers

  1. #1
    Registered User
    Join Date
    Aug 2015
    Posts
    75

    help in find prime numbers

    i want to print first 10 prime numbers.i tried following code but nothing prints. here is the code.
    can someone give me the code to print first 10 prime numbers nad also tell me whats wrong in my code

    Code:
    #include <stdio.h> 
    main() 
    { 
    int n,i=1,j,c,flag,count=0;
    for(i=2;i<=count;i++)
    {
        for(j=2;j<=i;j++)
        if(i%j==0)
        {flag=0;
        break;}
        if(i==j)
        goto t;
        
    }
    t:
    do
    {
        count++;
        printf("%d",i);
    }
    while(count<=10);
    }
    Last edited by san12345; 12-05-2015 at 02:28 AM.

  2. #2
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    a) The source code is not indented properly
    b) flag is not initialised and it's not even used apart from assigning the value 0 to it for some reason
    c) You have used goto (there are some valid reasons for using goto, this is not one of them)
    d) what does the do/while loop do?
    e) what does this program output and how does the output differ to what you expect the output to be?
    f) Why do you break out of the for loop just because i % j == 0?
    g) Why have to goto at all because t: is after the loop and you break; (which can never be reached) anyway?


    In summary, your program is this:

    Code:
    #include <stdio.h>
    int main(void)
    {
        int count = 0;
        int i = 2;
        do {
           count++;
           printf("%d", i);
        } while (count <= 10);
    }
    Does that look like what you wanted?

    Does your program output something that you didn't expect? If so, how is it different to what you expected?
    Last edited by Hodor; 12-05-2015 at 07:47 AM.

  3. #3
    Registered User
    Join Date
    Dec 2015
    Posts
    4
    Quote Originally Posted by san12345 View Post
    i want to print first 10 prime numbers.i tried following code but nothing prints. here is the code.
    can someone give me the code to print first 10 prime numbers nad also tell me whats wrong in my code

    Code:
    #include <stdio.h> 
    main() 
    { 
    int n,i=1,j,c,flag,count=0;
    for(i=2;i<=count;i++)
    {
        for(j=2;j<=i;j++)
        if(i%j==0)
        {flag=0;
        break;}
        if(i==j)
        goto t;
        
    }
    t:
    do
    {
        count++;
        printf("%d",i);
    }
    while(count<=10);
    }
    Code:
    void Prime(int max);
    
    
    void Prime(int max)
    {
        int loopCounter = 0;
        int loopCounter2 = 0;
        int prime = 0;
        int remainder = 0;
        int maxCheckCount = 0;  
        int maxCheck = 1;
        int numPrimes = 1;
        
        if (max >= 1)
            printf(" %d\n", 2);
            
        for (loopCounter = 3; numPrimes <= max; loopCounter += 2)
        {
            prime = 1;
            maxCheckCount++;
            if (maxCheckCount == maxCheck)
            {
                maxCheck++;
                maxCheckCount = 0;
            }
    
            for (loopCounter2 = 3; loopCounter2 <= maxCheck; loopCounter2 += 2)
            {
                remainder = loopCounter % loopCounter2;
                if (remainder == 0)
                {
                    prime = 0;
                    break;
                }
            }
    
            if (prime > 0)
            {
                printf(" %d\n", loopCounter);
                numPrimes++;
            }    
        }    
    }
    Last edited by pebmeister; 12-09-2015 at 04:21 PM. Reason: Change from max prime number to max number of primes

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 18
    Last Post: 03-30-2014, 06:30 AM
  2. Replies: 7
    Last Post: 01-09-2013, 07:41 PM
  3. Replies: 1
    Last Post: 03-16-2012, 02:07 AM
  4. for loop to find prime numbers
    By lsecrease in forum C Programming
    Replies: 2
    Last Post: 03-30-2006, 01:58 AM
  5. for loop to find prime numbers
    By 1rwhites in forum C Programming
    Replies: 11
    Last Post: 10-21-2005, 10:37 AM