Checking Prime Number in C++

By: Grant Braught Emailed: 1784 times Printed: 2614 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

This sample C++ program, simply accespt a number and indicates whether it is a Prime number or not a prime number.
#include 

// Prototype for IsPrime function.
bool IsPrime(int);

// main function is always run first in a
// C++ program.

int main()
{	//Given:   nothing.
	//Results: Accepts a number and
	//		  indicates if it is prime.

	int number;
	
	cout << "Enter an integer (>1): ";
	cin >> number;
	
	if (IsPrime(number))
	{
		cout << number << " is prime." << endl;
	}
	else
	{
		cout << number << " is not prime." << endl;
	}

	// This is more convention than anything.
	return 0;
}

bool IsPrime(int number)
{	// Given:   num an integer > 1
	// Returns: true if num is prime
	// 			false otherwise.
	
	int i;
	
	for (i=2; i<number; i++)
	{
		if (number % i == 0)
		{
			return false;
		}
	}
	
	return true;	
}

C++ Home | All C++ Tutorials | Latest C++ Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.



Bookmark and Share

Comments(3)


1. View Comment

werst program out put nt comming

View Tutorial          By: anu at 2012-08-02 13:09:00
2. View Comment

This program is mush faster and finer than given program.



#include <iostream>
#include <math.h>

bool IsPrime(int number)
{
for (int i=2; i<sqrt(number/2); i++)
{
if (number % i == 0)
{
return false;
}
}

return true;
}
int main()
{
int number;

cout << \"Enter an integer: \";
cin >> number;

if (IsPrime(number))
cout << number << \" is prime.\" << endl;
else
cout << number << \" is not prime.\" << endl;

return 0;
}


View Tutorial          By: Anonymous at 2013-02-27 05:38:27
3. View Comment

doesn't work with 3 , 7 and 5 but with numbers higher is works
from anonymous at 2013-02-27


View Tutorial          By: asd at 2013-05-11 09:25:30

Your name (required):


Your email(required, will not be shown to the public):


Your sites URL (optional):


Your comments:



More Tutorials by Grant Braught
Sum of 1 to N in C++
Vector variables in C++
Reverse a String in C++
Pass by Reference vs Pass Value in C++ functions
Pass by Reference in C++ functions
Checking Prime Number in C++
Insert, Delete, Search, Print an int Array in Java
Insert, Delete, Search, Print an int Array in Java
Palindrome String in Java

More Tutorials in C++
Two-Dimensional Array Manipulation in C++
Calculate average using Two-Dimensional Array in C++
Compute the square root of the sum of the squares of an array in C++
Matrix using nested for loops in C++
Sorting an array of Strings in C++
Calculating total based on the given quantity and price in C++
Compiling and Linking Multiple Source Files in C++
Enumerations in C++
Program to add two numbers in C++
Comments in C++
while loop in C++
for loop in C++
Programming errors a compiler will detect in C++
if in C++
Using the Built-in Arithmetic Types in C++

More Latest News
Most Viewed Articles (in C++ )
Using cout.width() in C++
Calculating total based on the given quantity and price in C++
Difference between Procedural, Structured, and Object-Oriented Programming
Dot (.) vs Arrow (->) to access data members in C++
Constructors and Destructors in C++
Calculate average using Two-Dimensional Array in C++
C++ Recursion function explained using Fibonacci series
assert() example program in C++
cin.ignore() in C++
Using peek() and putback() with cin in C++
Programming errors a compiler will detect in C++
Multiple statements in for loops in C++
Using Comments in a C++ Program
Using cout in C++
Sorting an array of Strings in C++
Most Emailed Articles (in C++)
Using cout.width() in C++
Two-Dimensional Array Manipulation in C++
Sorting an array of Strings in C++
Using cout in C++
Printing values in DEBUG mode in C++
File in C++ - Writing text to a file in C++
while loop in C++
Interface Versus Implementation in C++
C++ Sample Program for Allocating, using, and deleting pointers.
How to handle Exceptions in C++
Multiple inheritance example in C++
Vector variables in C++
Using the Built-in Arithmetic Types in C++
Compute the square root of the sum of the squares of an array in C++
Getting Started with C++