C++ Program to Print Prime Numbers

« Previous Program Next Program »

Print Prime Number in C++

To print all prime number between the particular range in C++ programming, check division from 2 to one less than that number (i.e., n-1), if the number is divided to any number from 2 to one less than that number then that number will not be prime. Otherwise, that number will be prime number as shown here in the following program.

C++ Programming Code to Print Prime Numbers

To print prime number between range for example, to print prime number between 3 to 29, here 3 is the starting number and 29 is the ending number, so following C++ program ask to the user to enter the starting and ending number to find and print all the prime number exists between the given range (here we are including the starting and ending number also if they are prime):

/* C++ Program - Print Prime Numbers */
void main()
	int start, end, i, j, count=0;
	// to print all the prime number between any range
	// enter the two number (starting and ending)
	cout<<"Enter starting number : ";
	cout<<"Enter ending number : ";
	cout<<"Prime Number Between "<<start<<" and "<<end<<" is :\n";
	for(i=start; i<=end; i++)
		for(j=2; j<i; j++)
			cout<<i<<" ";

When the above C++ program is compile and executed, it will produce the following result:

C++ program print prime numbers

Same Program in Other Language

You may like the same program in other programming languages:

« Previous Program Next Program »

Latest 5 Posts by CodesCracker


Quick Links
Signup - Login - Give Online Test