# Checking Prime Number in C++

By: Grant Braught Emailed: 1705 times Printed: 2206 times

 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; } ```

 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 #include bool IsPrime(int number) { for (int i=2; i> 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 email(required, will not be shown to the public):

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++ Using cout.fill() in C++ Programming errors a compiler will detect in C++ Calculating total based on the given quantity and price in C++ File in C++ - Writing text to a file in C++ assert() example program in C++ Using peek() and putback() with cin in C++ Specifying default values to function parameters in C++ C++ Recursion function explained using Fibonacci series Interface Versus Implementation in C++ C++ Sample Program for Allocating, using, and deleting pointers. C ++ Reference Parameters Versus Java Reference Parameters Converting C++ Multiple-Inheritance Hierarchies to Java Printing values in DEBUG mode in C++ cin.ignore() in C++
Most Emailed Articles (in C++)
 A Brief History of C++ atoi(), itoa() in C++ Character String Literals in C++ Lvalues and Rvalues in C++ Advanced if Statements in C++ Passing parameters to a function by value in C++ class keyword in C++ continue and break statements in C++ do...while Loops in C++ C++ Sample Program for Allocating, using, and deleting pointers. Converting Pointers that Operate on Arrays in C++ to Java Converting C++ Multiple-Inheritance Hierarchies to Java ctime() sample program in C++ Implementing Pure Virtual Functions in C++ Call by reference in C++ Functions