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.

Description :

Email: |

8951
algorithm
array
ASM program
binary tree
C/C++
C/C++ Programms
c graphics
c interview question
class
cpp
c program
c programs
C Programs
Data structure
download
engineering questions
free
frequently asked interview questions
Graphics
HDL
interview questions and answers
it interview questions
Java
Java program
Keyboard Interface
lab programs
linked list
Linked Lists
Microcontroller
microprocessor
program
programs
questions for an interview
Queue
source code
Source Codes
stack
Stepper motor
templates
top interview questions
verilog
Verilog program
Verilog programs
vlsi

This is the one stop educational site for all Electronic and Computer students. If you want to learn something new then we are here to help. We work on Microcontroller projects, Basic Electronics, Digital electronics, Computer projects and also in basic c/c++ programs.

#Home #Sitemap #Resources #Terms of Use

Copyright©2012 electrofriends.com All Rights Reserved

Contact:info@electrofriends.com

thank you

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

mistake in my program.

thankz.

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

Theres a logical bug!

This program cant find out all primes between 1 to 1000!!!!!!!!!!!!!!!!!!!!!!!!!!!!

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

}

}

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

thanks, this really helped me…

thanks this helped me alot ……

can u please help to do multiplication of matrix with possible easy way

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

Thanks

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

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

#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);

}

this ia a very good program easy to understand

#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();

}

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

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

}

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

thanks

nice program dude……..

nice programme by revathi!! its best one

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

}

good

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

}

}