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.
Description :
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 #Submit #Terms of Use
Copyright©2011 electrofriends.com All Rights Reserved
Contact:info@electrofriends.com | Powered by Dhyeya
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
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.
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