Program to find the primes upto entered number


#include<stdio.h>
main()
{
      unsigned index, pcnt=0;
      int max;
      printf("Enter integer N upto which primes are to be find :");
      scanf("%u",&max);
      if(max>0)
      {
         printf("Prim nos. upto %u.\n",max);
         {
         for(index=1;index<=max;index++)
            if(isprime(index))      //calling the function
            {
              printf("%u\n",index);
              pcnt++;
            }
         }
      }
      printf("\nNumber of primes = %u",pcnt);
      getch();
}
//function defining here  (main is giving 1 to 10 numbers
//"if max=10 in main" for the variable num)
int isprime(unsigned num)
{
    unsigned index,sq;
      if((num==1)||(num==2)||(num==3))
      {
      return 1;     //it returns true i.e.,num=1 or num=2 or num=3
      }
    else
      sq=sqrt((double)num);
      for(index=2;index<=sq;index++)
      {
         if(num%index==0)
         return 0;   //it returns false i.e.,nothing
      }
      return 1;  //it returns true i.e.,the value of num variable
}

Comments

Popular posts from this blog

Customize User's Profile in ASP.NET Identity System

Migrating database from ASP.NET Identity to ASP.NET Core Identity