Program to find the prime numbers between a given range

Thursday, December 4th, 2008

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.

Author Name :
Ranjith

Total : 25 Comments


25 Responses to “Program to find the prime numbers between a given range”

  1. nikhil vernekar says:

    thank you

  2. gaurav says:

    Thanks for your logic dude which helped me a lot in finding my little
    mistake in my program.

  3. preethi says:

    thankz.

  4. preethi says:

    This pgm doesn’t work if no prime numbers between the range or if two adjascent numbers are given.

  5. Shovan says:

    Theres a logical bug!
    This program cant find out all primes between 1 to 1000!!!!!!!!!!!!!!!!!!!!!!!!!!!!

  6. Rajneesh says:

    /*Thats the correct one*/

    #include
    main()
    {
    int m,n,i,j,,c;

    printf(“enter the number\n”);
    scanf(“%d %d”,&m,&n);
    c=0;
    for(i=m;i<=n;i++)
    {
    for(j=2;j<i;j++)
    {

    if(i==2)
    printf("%d",i);

    if(i%j==0)
    c++;

    }
    if(c==0)
    printf("%d",i);
    c=0;
    }

    }

  7. Merjil says:

    Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim N, i As Integer
    N = Val(TextBox1.Text)
    For i = 2 To N – 1
    If N Mod i = 0 Then
    MsgBox(“the number is not a prime number”)
    TextBox1.Text = “”
    TextBox1.Focus()
    Exit Sub
    End If
    Next i
    MsgBox(“the number is prime number”)
    TextBox1.Text = “”
    TextBox1.Focus()
    End Sub
    End Class

  8. rake says:

    thanks, this really helped me…

  9. krishna says:

    thanks this helped me alot ……
    can u please help to do multiplication of matrix with possible easy way

  10. venkatesh says:

    thanks for helping out me in this program. i tried before but i couldnt get the logic right.

  11. Nagesh Tanpure says:

    Thanks

  12. Magesh says:

    Thanks for the explaining the logic used. This is very useful as i can apply it for any lang..

  13. fatloss diet plan says:

    nice site I like the layout.Keep the good working! thanks!

  14. mahamad says:

    #include
    #include
    main()
    {
    int a,i,j,c;
    printf(“Enter Number:=>”);
    scanf(“%d”,&a);
    if(a2)
    for(i=2;i<a;i++)
    {
    if(a%i==0)
    j=0;
    }
    if(j==0)
    printf("%d is not a prime number\n",a);
    else
    printf("%d is a prime number\n",a);
    }

  15. Dharmendra malviya says:

    this ia a very good program easy to understand

  16. Revathi Lakshmi says:

    #include
    #include
    void main()
    {
    int i,n1,n2,count=0;
    printf(“Enter the starting no.”);
    scanf(“%d”,&n1);
    printf(“Enter the ending no.”);
    scanf(“%d”,&n2);
    while(n1<=n2)
    {
    for(i=2;i<=n1;i++)
    {
    if(n1%i==0)
    {
    count=count+1;
    }
    }
    if(count==0)
    {
    printf("%d",n1);
    }
    count=0;
    n1++;
    }
    getch();
    }

  17. Aarthi says:

    this program is short and easily understandable…and it was a timely help

  18. subhash says:

    #include

    main()
    {
    int n, i = 3, count, c;

    printf(“Enter the number of prime numbers required\n”);
    scanf(“%d”,&n);

    if ( n >= 1 )
    {
    printf(“First %d prime numbers are :\n”,n);
    printf(“2\n”);
    }

    for ( count = 2 ; count <= n ; )
    {
    for ( c = 2 ; c <= i – 1 ; c++ )
    {
    if ( i%c == 0 )
    break;
    }
    if ( c == i )
    {
    printf("%d\n",i);
    count++;
    }
    i++;
    }

    return 0;
    }

  19. Sam says:

    thanx a lot.. pls help me wid a prog to convert a num to its binary form..

  20. keerti says:

    thanks

  21. HABEETH says:

    nice program dude……..

  22. omkar says:

    nice programme by revathi!! its best one

  23. Apexit says:

    #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:

    }

  24. Prime numbers and sum of non prime numbers between 1 to 100 says:

    //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

Question and Answer
C/C++ Unix & Linux Wordpress
Source codes
C C++ Java

Free email signup

Email: