C Program to print the prime numbers between two numbers.

Prime numbers between two numbers :

PrimeNumbers.c
#include<stdio.h> 
#include<math.h> 
void printPrimes(int num1,int num2); 
int isPrimeNumber(int n); 
int main(void) 
{ 
    int num1,num2; 
    printf("Enter any two numbers : "); 
    scanf("%d%d",&num1,&num2); 
    printf("The prime numbers between %d and %d are : ",num1,num2); 
    printPrimes(num1,num2); 
    return 0; 
} 
void printPrimes(int num1, int num2) 
{ 
    int i; 
    for(i=num1; i<=num2; i++) 
           if(isPrimeNumber(i)) 
            printf("%d ",i); 
} 
int isPrimeNumber(int n) 
{ 
    int i; 
    for(i=2; i<=sqrt(n); i++)     
        if(n%i==0) 
            return 0; 
    return 1; 
}

Output:

Terminal
Enter any two numbers :
1
20
The prime numbers between 1 and 20 are : 1 2 3 5 7 11 13 17 19

Enter any two numbers :

1
10
The prime numbers between 1 and 10 are : 1 2 3 5 7

Happy Learning 🙂

About the Author:

Avatar
Founder of onlinetutorialspoint.com Love Java, Python, Shell and opensource frameworks. Follow him on twitter and facebook for latest updates.

Leave A Comment