C program to find prime number from given intervals

Write c program to take numbers from the user and print all prime numbers between the given range.

C program to print all prime numbers between two Intervals

In this program, taking lower range and higher range from the user and display all prime numbers between a given range.

#include<stdio.h>
int main()
{
    int i,j,LR,HR,b;
    printf("enter the lower range:");
    scanf("%d",&LR);
    printf("enter the higher range:");
    scanf("%d",&HR);
    printf("prime numbers between %d to %d are:",LR,HR);
    for(i=LR;i<=HR;i++)
    {      
        b=0;
        for(j=1;j<=i;j++)
        {
            if(i%j==0)
            {
                b=b+1;
            }
        }
        if(b==2)
            printf("%d \t",i);
    }
    return 0;
}

output

enter the lower range:10
enter the higher range:20
prime numbers between 10 to 20 are:11   13  17  19

Logic to Find prime number between given Interval

  • In this program, you will find all prime numbers between the given user's lower range and higher range.
  • You will take lower-range i.e LR and higher-range i.e UR from the user and store them in an LR and HR.
  • Iterate the outer loop from LR to UR, on each iteration of the outer loop, you will find whether the number is prime or not with the help of the inner loop.
  • If the number is prime, you will print the current iteration number and goes for the next iteration of the outer loop.

C program to print all prime numbers between 1 to given range

In this program, taking higher range from user and print all prime number between 1 to given range.

#include<stdio.h>
int main()
{
    int i,j,n,b;
    printf("enter the higher range:");
    scanf("%d",&n);
    printf("\n prime number are:");
    for(i=1;i<=n;i++)
    {  
        b=0;
    	for(j=1;j<=i;j++)
        {
            if(i%j==0)
            {
            	b=b+1;
            }
        }
        if(b==2)
            printf("%d \t",i);
    }
    return 0;
}

output

enter the higher range:10
prime number are:2  3   5   7