/* ************************************************************************** * Program name : 012_Prime_number_or_not (Version 1.00) * * Author : Duck Wong * * Language : C / C++ * * Compiler : Boodshed Dec-C++ compiler Ver 3.95 * * Computer : PII350 * * O/S : Windows 98 * ************************************************************************** * Version 1.00 : 2000/06/08 - first version * ************************************************************************** * Description : (a) Input an integer number * * (b) Is that a prime number ? * * (c) Output results * ************************************************************************** */ #include #include int main() { // part 1 : declaration int Inputnum, Index; char Status, again; // part 2 do { // (a) input an integer do { cout << "Please enter an integer number : "; cin >> Inputnum; } while (Inputnum<=0); // Note (1) // (b) check the status Status='N'; for (Index=2; Index<=((Inputnum+1)/2); Index++) { if ((Inputnum % Index)== 0) // Note (2) { Status='Y'; break; } }; // (c) output if (Status=='Y' ? cout << Inputnum << " is not a prime number" : cout << Inputnum << " is a prime number"); // Note (3) // (d) try another number ? cout << "\a\n\nTry another number (Y/N) : "; cin >> again; } while (again=='Y' || again=='y'); cout << "\n\n" << endl; system("PAUSE"); return 0; } /* Notes (1) The inputed integer number must be greater than zero. (2) If the remainder of Inputnum over Index is zero, then Inputnum is not a prime number. (3) operator ?: example : if (Status=='Y' ? cout << Inputnum << " is not a prime number" : cout << Inputnum << " is a prime number"); is eaual to the following statements : if (Status=='Y') cout << Inputnum << " is not a prime number"; else cout << Inputnum << " is a prime number"; */