Program to find the prime numbers between a given range

The program is to find all the prime numbers falls inside the user defined range. A prime number is a one, whose divisors are 1 and the number itself.

Logic: This is advanced version of the previous program. Here, user need to enter two numbers as the lower and upper limits for the iteration loop to find the prime number in between. The outer for loop traces the iteration till the limit, wherein each of iteration inner for loop checks the present number is prime or not, with the prime number’s logic. If it is, it prints out the present number.

In this program, both the lower limit and the upper limit are variables, and so is flexible. These three programs show, how we can upgrade the logic to make the code flexible one.

The similar growth can be seen in the case of finding the perfect numbers.

Program to find the prime numbers between a given range

#include<stdio.h>
void main()
{
int i, prime, lim_up, lim_low, n;
clrscr();
printf(“\n\n\t ENTER THE LOWER LIMIT…: “);
scanf(“%d”, &lim_low);
printf(“\n\n\t ENTER THE UPPER LIMIT…: “);
scanf(“%d”, &lim_up);
printf(“\n\n\t PRIME NUMBERS ARE…: “);
for(n=lim_low+1; n<lim_up; n++)
{
prime = 1;
for(i=2; i<n; i++)
if(n%i == 0)
{
prime = 0;
break;
}
if(prime)
printf(“\n\n\t\t\t%d”, n);
}
getch();
}
Download exe and source code here.

Editorial Team
Editorial Team

We are a group of young techies trying to provide the best study material for all Electronic and Computer science students. We are publishing Microcontroller projects, Basic Electronics, Digital Electronics, Computer projects and also c/c++, java programs.

25 thoughts on “Program to find the prime numbers between a given range

  1. #include
    void main()
    {
    int i,n,a,b,j=0;
    printf(“enter range in which you want to find out the prime numbers seperated by space: “);
    scanf(“%d%d”,&a,&b);
    if(a<0||b<0)
    {
    printf("number is negative");
    goto there;
    }
    printf("prime number in entered range are=\n");
    for(n=a;n<=b;n++)
    {
    for(i=2;i<=(n/2);i++)
    {
    if (n%i==0)
    j++;
    }
    if(j==0)
    printf("%d ",n);
    j=0;
    }
    there:

    }

  2. //sudheer.k mulapadava

    class A
    {
    public static void main(String[] args)
    {
    int count=0;int sum=0;
    for (int i=2;i<100 ;i++ )
    {
    for (int j=2;j<i ;j++ )
    {
    if(i%j==0)
    count++;
    }

    if(count==0)
    System.out.println("Prime numbers are :"+i);
    else
    {

    sum=sum+i;
    }
    count=0;

    }
    System.out.println(" Sum of non Prime numbers are :"+sum);
    }
    }

Leave a Reply

Your email address will not be published. Required fields are marked *

Get the latest updates on your inbox

Be the first to receive the latest updates from Codesdoc by signing up to our email subscription.

    StudentProjects.in