codescracker


c++

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 */
		
#include<iostream.h>
#include<conio.h>
void main()
{
	clrscr();
	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 : ";
	cin>>start;
	cout<<"Enter ending number : ";
	cin>>end;
	cout<<"Prime Number Between "<<start<<" and "<<end<<" is :\n";
	for(i=start; i<=end; i++)
	{
		count=0;
		for(j=2; j<i; j++)
		{
			if(i%j==0)
			{
				count++;
				break;
			}
		}
		if(count==0)
		{
			cout<<i<<" ";
		}
	}
	getch();
}

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 »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test