Wednesday 24 June 2015

c program to print Prime Numbers Between 1 and N


//  c program to print Prime Numbers Between 1 and N 

#include<stdio.h>
void main()
{
 int num,i=1,j,count;
         //clrscr(); 
 printf("Enter Num value To Print Prime Numbers between 1 and Num: ");
 scanf("%d",&num);
 printf("Prime Numbers upto %d :\n \n",num);

 while(i<=num)
 {
  count=0;
  for(j=1;j<=i;j++)
  {
   if(i%j==0)   //checking whether num is dvisible by j
   count++;
  }
  if(count==2)   //if num is divisible by 2 numbers,then it is prime
   printf("%d ",i);
   i++;
 }
 printf("\n\n");
             //getch(); 
}
----------------------------------------------------------------------
output :
-----------------------------------------------------------------------
root@AHDFEWPU0038:/home/rjas/Desktop# gcc code.c 
root@AHDFEWPU0038:/home/rjas/Desktop# ./a.out 
Enter Num value To Print Prime Numbers between 1 and Num: 25
Prime Numbers upto 25 :
 
2 3 5 7 11 13 17 19 23 

No comments: