Simple Logic For Finding Prime Number is primarility test is 2 to root of n..Source: Wikipedia

The property of being prime is called primality. A simple but slow method of verifying the primality of a given number

nis known as trial division. It consists of testing whethernis a multiple of any integer between 2 and {\displaystyle {\sqrt {n}}}. Algorithms much more efficient than trial division have been devised to test the primality of large numbers. These include the Miller–Rabin primality test, which is fast but has a small probability of error, and the AKS primality test, which always produces the correct answer in polynomial time but is too slow to be practical. Particularly fast methods are available for numbers of special forms, such as Mersenne numbers. As of January 2016, the largest known prime number has 22,338,618 decimal digits.

simple code:

/* Program to check prime number or not in c */ #include<stdio.h> #include<math.h> int isPrime(int n) { int i; for(i=2; i<sqrt(n); i++) //here primarility test logic is using here 2 to sqrt(n)..details can be found in wikipedia { if(n%i==0) { return 0; } } return 1; } int main() { int count; for(count=10; count<=100; count++) { if(isPrime(count)==1) { printf(" %d is prime\n",count); } // else if(isPrime(count)==0) // { // printf("no %d is not prime\n",count); // } } return 0; }

Another Code That Can Also show up the sum of the primes.

/* Program to check prime number or not in c */ #include<stdio.h> #include<math.h> int isPrime(int n) { int i; for(i=2; i<sqrt(n); i++) //here primarility test logic is using here 2 to sqrt(n)..details can be found in wikipedia { if(n%i==0) { return 0; } } return 1; } int sumOfPrimes(int lb,int ub){ int count,sum; sum=0; for(count=lb;count<=ub;count++) { if(isPrime(count)==1){ sum=sum+count; } } return sum; } int main() { int count; for(count=10; count<=100; count++) { if(isPrime(count)==1) { printf(" %d is prime\n",count); } // else if(isPrime(count)==0) // { // printf("no %d is not prime\n",count); // } } int s; s=sumOfPrimes(10,100); printf("Sum of primes in the rang %d-%d is: %d\n",10,100,s); return 0; }

Playing With The Logic: Slightly Changed in Return Value 😉 😀

code:

/* Program to check prime number or not in c */ #include<stdio.h> #include<math.h> int isPrime(int n) { int i; for(i=2; i<sqrt(n); i++) //here primarility test logic is using here 2 to sqrt(n)..details can be found in wikipedia { if(n%i==0) { return 2; } } return 1; } int sumOfPrimes(int lb,int ub){ int count,sum; sum=0; for(count=lb;count<=ub;count++) { if(isPrime(count)==1){ sum=sum+count; } } return sum; } int main() { int count; for(count=10; count<=100; count++) { if(isPrime(count)==1) { printf(" %d is prime\n",count); } // else if(isPrime(count)==0) // { // printf("no %d is not prime\n",count); // } } int s; s=sumOfPrimes(10,100); printf("Sum of primes in the rang %d-%d is: %d\n",10,100,s); return 0; }

Made the code more dynamic and taking input from users: 😀

/* Program to check prime number or not in c */ #include<stdio.h> #include<math.h> int isPrime(int n) { int i; for(i=2; i<sqrt(n); i++) //here primarility test logic is using here 2 to sqrt(n)..details can be found in wikipedia { if(n%i==0) { return 2; } } return 1; } int sumOfPrimes(int lb,int ub){ int count,sum; sum=0; for(count=lb;count<=ub;count++) { if(isPrime(count)==1){ sum=sum+count; } } return sum; } int main() { int i,lb,ub; printf("Enter lower bound: "); scanf("%d",&lb); printf("Enter upper bound: "); scanf("%d",&ub); for(i=lb; i<=ub; i++) { if(isPrime(i)==1) { printf(" %d is prime\n",i); } // else if(isPrime(count)==0) // { // printf("no %d is not prime\n",count); // } } int s; s=sumOfPrimes(lb,ub); printf("Sum of primes in the rang %d-%d is: %d\n",lb,ub,s); return 0; }

Vid explained: