Thursday 26 January 2012

Write a C program to genarate all the prime numbers between 1 to 'n'

#include<stdio.h>
#include<conio.h>
void main()
{
    int st, n, x, flag;
    clrscr();
    printf("Enter the value of 'n': ");
    scanf("%d", &n);
    for(st=1; st<=n; st++)
    {
        x=2; flag=0;
        while(x<=st/2)
        {
            if(st%x==0)
            {
                flag=1; break;
            }
            x++;
        }
        if( flag==0)
        printf("%5d", st);
    }
    getch();
}

No comments:

Post a Comment