Checking Prime Number in C++
By: Grant Braught
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;
}
Comment on this tutorial
- Data Science
- Android
- AJAX
- ASP.net
- C
- C++
- C#
- Cocoa
- Cloud Computing
- HTML5
- Java
- Javascript
- JSF
- JSP
- J2ME
- Java Beans
- EJB
- JDBC
- Linux
- Mac OS X
- iPhone
- MySQL
- Office 365
- Perl
- PHP
- Python
- Ruby
- VB.net
- Hibernate
- Struts
- SAP
- Trends
- Tech Reviews
- WebServices
- XML
- Certification
- Interview
categories
Subscribe to Tutorials
Related Tutorials
Calculating total based on the given quantity and price in C++
Sorting an array of Strings in C++
Matrix using nested for loops in C++
Compute the square root of the sum of the squares of an array in C++
Calculate average using Two-Dimensional Array in C++
Two-Dimensional Array Manipulation in C++
Compiling and Linking Multiple Source Files in C++
Escape Sequences for Nonprintable Characters in C++
Using the Built-in Arithmetic Types in C++
Archived Comments
1. werst program out put nt comming
View Tutorial By: anu at 2012-08-02 13:09:00
2. This program is mush faster and finer than given p
View Tutorial By: Anonymous at 2013-02-27 05:38:27
3. doesn't work with 3 , 7 and 5 but with numbers hig
View Tutorial By: asd at 2013-05-11 09:25:30