C Program to find Prime Numbers within a Range

This program prints prime numbers within a range. If you want to check a number is prime or not, you may visit this link.

/*Prime Numbers within a Range*/

#include <stdio.h>
#include <math.h>

int main()
{
    int from, to, is_prime, i , j;
    printf("Enter the first number: ");
    scanf("%d",&from);
    printf("Enter the last number: ");
    scanf("%d",&to);
    printf("Prime numbers from %d to %d are:\n",from,to);
    for(i=from; i<=to ; i++){
    is_prime = 1;
    for(j=2;j<=sqrt(i);j++){
        if(i%j==0){
            is_prime = 0;
            break;
        }
        else
            is_prime = 1;
        }
    if(i<2)
        is_prime = 0;
    if(is_prime == 1)
        printf("%4d ",i);
    }
    return 0;
}

0 comments :

Post a Comment

Spam comments will be deleted. :)

 
Loading...
TOP