C program to generate all the prime numbers between 1 and n

Written by siddhartha. Posted in Basics

Source Code

/*************************************************************************************/
/* Write a C program to generate all the prime numbers between 1 and n, where n is a value supplied by the user. */
/*  Download more programs at http://sourcecode4u.com/ */
/*************************************************************************************/

#include <stdio.h>
void main()
{
    int no,counter,counter1,check;
    printf("PRIME NO. SERIES");
    printf("\nINPUT THE VALUE OF N: ");
    scanf("%d",&no);
    printf("\n\nTHE PRIME NO. SERIES B/W 1 TO %d : \n\n",no);
    for(counter = 1; counter <= no; counter++)
    {
        check = 0;
        //THIS LOOP WILL CHECK A NO TO BE PRIME NO. OR NOT.
        for(counter1 = counter-1; counter1 > 1 ; counter1--)
        if(counter%counter1 == 0)
        {
            check++;        // INCREMENT CHECK IF NO. IS NOT A PRIME NO.
            break;
        }
        if(check == 0)
        printf("%d\t",counter);
    }
    printf("\n");
}

Out Put

output for prime number generation
BLOG COMMENTS POWERED BY DISQUS