Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <iomanip>
- using namespace std;
- bool isPrime(int number)
- {
- for (int divisor = 2; divisor <= number / 2; divisor++)
- {
- if (number % divisor == 0)
- {
- return false;
- }
- }
- return true;
- }
- //-----------------------------------------------------------------------------------------------------------------------
- int PrimeNumber(int number)
- {
- if (isPrime(number))
- {
- return number;
- }
- }
- //---------------------------------------------------------------------------------------------------------------------------------
- char digitToChar(int d)
- {
- return static_cast<char>(d + '0');
- }
- //---------------------------------------------------------------------------------------------------------------------------------------------
- string intToString(int number)
- {
- string s = "";
- while (PrimeNumber(number) > 0)
- {
- s = digitToChar(number % 10) + s;
- number = number / 10;
- }
- if (s == "")
- s = digitToChar(0);
- return s;
- }
- //-----------------------------------------------------------------------------------------------------------------------------------------------
- void printPALS(int numberOfPalendromes)
- {
- bool isPalendrome = true;
- {
- const int NUMBER_OF_PALS_PER_LINE = 10;
- int count = 0;
- int number = 2;
- while (count < numberOfPalendromes)
- {
- //Plugs number into string function, which checks for prime before sending back
- string pal = intToString(number);
- int low = 0;
- int high = pal.length() - 1;
- //Checks for palendrome. If it isn't, breaks and checks next prime.
- while (low < high)
- {
- if (pal[low] != pal[high])
- {
- isPalendrome = false;
- break;
- }
- low++;
- high--;
- }
- //If it is a palendrome, starts putting them in columns until 100
- if (isPalendrome)
- {
- count++;
- if (count == NUMBER_OF_PALS_PER_LINE)
- {
- cout << setw(4) << number << endl;
- }
- else
- {
- cout << setw(4) << number;
- }
- number++;
- }
- }
- }
- }
- int main()
- {
- cout << "The first 100 Prime Pal numbers are \n";
- printPALS(100);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment