/* isPrime Function - A prime number is a number that is only evenlydivisible by itself and 1. For example, the number 5 is primebecause it can only be evenly divided by 1 and 5. The number 6,however, is not prime because it can be divided evenly by 1, 2, 3and 6.The following main function is used:* isPrime()The following additional functions are used:* showMenu()* showIntro()* getData()* displayResult()The function is demonstrated in this program. */#include "Utility.h"/* Prototypes: Is prime, Show menu, Show Intro, Get data, Display result */bool isPrime(int);void showMenu();void showIntro();void getData();void displayResult(int);int main(){showMenu();getData();return 0;}/* **********************************************************Definition: showMenuThis function shows a small menu********************************************************** */void showMenu(){/* Variable: Again, Introduction (initialized to 'I') */char intro = ' ',again = ' ';/* * Ask if the user wants to view a short introduction tothe program* Call: showIntro, catchInfiniteLoop* Validate: Menu choice */cout << "\t\tPrimality Test\n\n"<< "Do you wish to view a short introduction (Y/N)? ";cin >> intro;if (intro == 'y'){showIntro();}while (!(intro == 'y' || (intro == 'n'))){cout << "\nInvalid selection. Please enter 'y' if you wish to view an\n"<< "introduction, or 'n' if you wish to start right away.\n";cout << "Please select: ";cin >> intro;}}/* **********************************************************Definition: showIntroshowIntro displays a short introduction, so that the usercan learn about the functionality and purpose of thisprogram.********************************************************** */void showIntro(){cout << "\n\t\tPrimality Test - Introduction\n\n"<< "This program uses a custom function to test a number for\n"<< "primality. The result is returned as boolean value. In other\n"<< "words:\n\n"<< " * If a number is a prime number, the result returned\n"<< " is true\n"<< " * If a number is not a prime number, the result returned\n"<< " is false.\n\n"<< "The following simple rules are applied:\n\n"<< " * 1. Is the number % 2, 3 ... n-1 equal to 0\t| Yes ? not prime |\n"<< " * 2. Is the number entered <= 1\t\t| Yes ? not prime |\n"<< " * 3. Is the number entered divisible by itself | Yes ? is prime |\n\n"<< "Once one of these conditions is met, the result will be returned\n"<< "and displayed. As long as you don't choose to quit, you can test\n"<< "any positive number for primality.\n";}/* **********************************************************Definition: getDataThis function asks for a number to be tested, then callsdisplayResult, to display the result.********************************************************** */void getData(){/* Variable: Number */int number = 0,displayIntroduction = 0;char again = ' ';/* As long as the user wishes not to quit, he or she is askedto enter a number to test for primality.Get: Number, Choice to continue or quitCall: catchInifiniteLoop, displayResultValidate: Input, User choice */do{cout << "\nEnter a number and I will tell you if it is prime: ";cin >> number;cout << endl;while (number < 0){cout << "\nThe entered number is invalid. Please enter a positive\n"<< "integer value (1 or above): ";cin >> number;catchInfiniteLoop();}displayResult(number);cout << "\nDo you wish to try another number? "<< "(Enter 'y' to continue or 'n' to quit): ";cin >> again;while (!(again == 'y' || (again == 'n'))){cout << "\nInvalid menu choice!\n"<< "Do you wish to try another number? ";cin >> again;catchInfiniteLoop();}if (again == 'n'){cout << "\nThank you for testing this program!\n"<< "(Press Enter to exit)\n";pauseSystem();}} while (again != 'n');}/* **********************************************************Definition: isPrimeThis function takes an integer value as argument and testsit for primality. If the number is prime, it returns true,else it returns false.********************************************************** */bool isPrime(int primeNumber){/* Variable: Prime test (initialized to true) */bool primeTest = true;/* If any number lower than or equal to 1 is entered,primeTest gets false, and the result false is returned */if (primeNumber <= 1){return primeTest = false;}/* primeNumber is tested until one of the followingcondition is met:1. primeNumber % divisor (2, 3 ... n-1) equals 0,which means that the number is not prime, soprimeTest gets false, and the result false isreturned2. primeNumber is divisible only by itself, thenthe loop exits, primeTest gets true, and trueis returnedSince divisior is already initialized to 2, and 2 isthe only even number that is prime, this case istaken care of and 2 is displayed as prime number */for (int divisor = 2; divisor < primeNumber; divisor++){/* Display: The steps it takes until one of the conditionsis met. (Should be deleted or commented outwhen greater numbers are tested that areprime ...) */cout << "Number: " << primeNumber << " % " << divisor << "\n";if (primeNumber % divisor == 0){return primeTest = false;}}/* Return: primeTest true if number is prime */return primeTest = true;}/* *******************************************************Definition: displayResultThis function takes the result of primeTest as argumentand displays whether the number is prime or not.******************************************************* */void displayResult(int prime){if (isPrime(prime)){cout << "\n" << prime << " is a prime number.\n";}else{cout << "\n" << prime << " is not a prime number.\n";}}
Sunday, January 15, 2017
Programming Challenge 6.22 - isPrime Function
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment