Thursday 15 August 2019

C program to find the largest prime number below 5000, test a prime number


/*to find the largest  prime number below 5000*/
#include<stdio.h>
#include<math.h>
void
main()
{
                int n,i,remainder,d,c=0;
                for(n=5000;n>=3;n--)
                {
                                d=sqrt(n);
   for(i=2;i<d;i++)
   {
                   remainder=n%i;
                   if(remainder==0)

                                   goto label;
                   }
                   if(n>c)
                                   c=n;
   }
label:;
                }
                printf("the largest prime number %d is\n",c);
}
Result:
the largest prime number 4999 is
Press any key to continue



/*test a prime number (2^(2^n))+1  */
#include<stdio.h>
#include<math.h>
void
main()
{
                int n,i,remainder,d,m1,m;
                for(n=1;n<=4;n++)
                {
                                m1=(pow(2,n));
                                m=(pow(2,m1)+1);
                                d=sqrt(m);
                                for(i=2;i<d;i++)
                                {
                                                remainder=m%i;
                                                if(remainder==0)
                                                {
                                                                goto label;
                                }
                                }
                                printf("the number %d is prime\n",m);
label:;
                }
}
Result:
the number 5 is prime
the number 17 is prime
the number 257 is prime
the number 65537 is prime
Press any key to continue

No comments:

Post a Comment

Thanks for comment.