C Program to print all prime numbers within given number


#include<stdio.h>
main()
{
  int num,i,count=0,j;
  printf("Enter a number \n");
  scanf("%d",&num);
  printf("The Prime numbers upto %d are\n",num);
  for(i=1;i<=num;i++)
  {
    count=0;
    for(j=1;j<=i;j++)
    {
      if(i%j==0)
      {
        count++;
      }
    }
    if(count==2)
    {
      printf("%d\n",i);
    }
  }
}
Enter a number
30
The Prime numbers upto 30 are
2
3
5
7
11
13
17
19
23
29

Post a Comment

If you have any doubts, Please let me know
Thanks!

Previous Post Next Post