Program to Show Prime Numbers between Range

This article shows how to write a C or C++ program to show prime numbers between a Range. Before start let us discuss about the prime numbers.

What is Prime Number?
In mathematics Prime Number are those number whose can be divided evenly only by 1, or itself. It must be a whole number greater than 1. For example: 5 can only be divided evenly by 1 or 5, so it is a prime number. 6 can be divided evenly by 1, 2, 3 and 6. So 6 is not a prime number. it is a composite number.

The first few prime numbers are 2, 3, 5, 7, 11, 13, 17, 19, 23. There is no largest prime number. For every prime number p there exists a prime number p’ such that p’ is greater than p. This number was demonstrated in ancient times by the Greek mathematician Euclid.

Program to Show Prime Numbers between Range
#include
#include
#include
int main(){
clrscr();
int i,j,n,f,k=0;     cout<<”      ” This program will show Prime numbers between a range. “”<<endl<<endl;

xx:
cout<<“How many numbers ? Give a integer number.”<<endl;
cin>>n;

if(n<0) {
cout<<“Negative number not allow,Please give a positive number.”<<endl<<endl;
goto xx;
}

cout<<endl;
cout<<“Prime numbers between 1 to “<<n<<” are :”<<endl;

for(i=1;i<=n;i++) {
f=0;
for(j=2;j<i;j++)
if ((i%j)==0) f=1;
if (f==0) {
cout<<i<<”  “;
k++;
}
}

if (k==0) {
cout<<0;
}

getch();
}

Comments

  1. By Roy

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *