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(“nnt ENTER THE LOWER LIMIT…: “);
scanf(“%d”, &lim_low);
printf(“nnt ENTER THE UPPER LIMIT…: “);
scanf(“%d”, &lim_up);
printf(“nnt 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(“nnttt%d”, n);
}
getch();
}
Download exe and source code here.

Avatar Image

Author Name :
Ranjith

Total : 13 Comments


13 Responses to Program to find the prime numbers between a given range

  1. anonymous

    This program totally sucks.

    It won’t even compile in Linux. clrscr needs conio.h

    Its also brute force search.
    You can find better algos in places like this for example http://wwwhomes.uni-bielefeld.de/achim/prime_sieve.html

  2. Kimabroobbind

    Good day! Prompt, where it is possible to download game HL2 with new mods.
    I heard, that there many new opportunities. It is thankful in advance.

  3. nikhil vernekar

    thank you

  4. gaurav

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

  5. preethi

    thankz.

  6. preethi

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

  7. Shovan

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

  8. Rajneesh

    /*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;
    }

    }

  9. Merjil

    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

  10. rake

    thanks, this really helped me…

  11. krishna

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

  12. venkatesh

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

  13. Nagesh Tanpure

    Thanks

Leave a Reply

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

Free email signup