C program to find prime numbers between two numbers





C program or code to find prime numbers between two numbers


#include<stdio.h>

int main(){

    int num,i,count,min,max;

     printf("Enter min range: ");
     scanf("%d",&min);

    printf("Enter max range: ");
    scanf("%d",&max);

    for(num = min;num<=max;num++){

         count = 0;

         for(i=2;i<=num/2;i++){
             if(num%i==0){
                 count++;
                 break;
             }
        }
        
         if(count==0 && num!= 1)
             printf("%d ",num);
    }
  
   return 0;
}

Sample output:
Enter min range: 10
Enter max range: 50
11 13 17 19 23 29 31 37 41 43 47


Algorithm:


Definition of prime number:

A prime number is a natural number greater than one that possesses only two divisors: 1 and itself. In essence, it is not divisible by any other numbers except these two. As an illustration, the number 5 exemplifies a prime number, as its only divisors are 1 and 5.

Their divisors are 1 and 5.

Note: 2 is only even prime number.

Logic for prime number in c

We employ a loop to divide the given number by integers from 2 to the square root of the number. If, at any point, the number is divisible without a remainder, it is not a prime number. Otherwise, it qualifies as a prime number."

This approach can improve the efficiency of your prime number checking algorithm.


Example of prime numbers : 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199 etc.



3 comments:

Unknown said...

change i!=0 with i>1, will work with neg integers too

Sushant Kumar said...

try this also
#include
#include
using namespace std;
int main()
{
int a,i,n,j,b,k=0;
cout<<"enter the first range :";
cin>>a;
cout<<"enter the last range :";
cin>>b;
for(i=a;i<=b;i++)
{
k=0;
for(j=1;j<=i;j++)
{
n=i%j;
if(n==0)
{
k=k+1;
if(k==2 && i==j )
{
cout<<"i="<<i<<endl;
}
}

}

}
return 0;
getch();

}

DEVRAJ ANAND said...

why do wetake count as variable