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
August 7th, 2009 at 10:49 pm
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
April 8th, 2010 at 1:56 am
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.
February 7th, 2011 at 11:01 am
thank you
February 21st, 2011 at 5:24 pm
Thanks for your logic dude which helped me a lot in finding my little
mistake in my program.
March 30th, 2011 at 8:17 pm
thankz.
March 30th, 2011 at 8:18 pm
This pgm doesn’t work if no prime numbers between the range or if two adjascent numbers are given.
June 10th, 2011 at 10:07 pm
Theres a logical bug!
This program cant find out all primes between 1 to 1000!!!!!!!!!!!!!!!!!!!!!!!!!!!!
October 7th, 2011 at 6:19 pm
/*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;
}
}
November 4th, 2011 at 4:26 pm
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
December 3rd, 2011 at 9:24 pm
thanks, this really helped me…
December 19th, 2011 at 10:49 pm
thanks this helped me alot ……
can u please help to do multiplication of matrix with possible easy way
January 9th, 2012 at 8:02 pm
thanks for helping out me in this program. i tried before but i couldnt get the logic right.
January 27th, 2012 at 6:04 pm
Thanks