2/22/2009

C Program To Print Prime Numbers Up To n



/*C Program To Print Prime Numbers Up To n*/

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int num,i,count,n;
    printf("Enter the value:\n");
    scanf("%d",&n);

    printf("Prime numbers up to %d are: \n",n);
    printf("---------------------------\n");
    for(num = 1;num<=n;num++){

         count = 0;

         for(i=2;i<=num/2;i++){
             if(num%i==0){
                 count++;
                 break;
             }
        }
        if(count==0 && num!= 1)
             printf("%d \n",num);
    }


   return 0;

}
Out Put:
Enter the value:
11
Prime numbers up to 11 are:
---------------------------
2
3
5
7
11

No comments:

Post a Comment