Advertisement
justinpuckett

Untitled

May 26th, 2015
263
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.44 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <iomanip>
  4. using namespace std;
  5.  
  6. bool isPrime(int number)
  7. {
  8.     for (int divisor = 2; divisor <= number / 2; divisor++)
  9.     {
  10.         if (number % divisor == 0)
  11.         {
  12.             return false;
  13.         }
  14.     }
  15.     return true;
  16. }
  17. //-------------------------------------------------------------
  18.  
  19. char digitToChar(int d)
  20. {
  21.     return static_cast<char>(d + '0');
  22. }
  23.  
  24. //-------------------------------------------------------------
  25. string intToString(int number)
  26. {
  27.     string s = "";
  28.  
  29.     while (number > 0)
  30.     {
  31.         s = digitToChar(number % 10) + s;
  32.         number = number / 10;
  33.     }
  34.     if (s == "")
  35.         s = digitToChar(0);
  36.     return s;
  37. }
  38.  
  39. //---------------------------------------------------------------
  40. bool isPalendrome(int number)
  41. {
  42.     string s = intToString(number);
  43.     int low = 0;
  44.     int high = s.length() - 1;
  45.  
  46.     while (low < high)
  47.     {
  48.         if (s[low] != s[high])
  49.         {
  50.             return false;
  51.             break;
  52.         }
  53.  
  54.         low++;
  55.         high--;
  56.     }
  57.  
  58. }
  59.  
  60. //------------------------------------------------------------------
  61. void printTable(int points)
  62. {
  63.     const int PAP = 10;
  64.     int count = 0;
  65.     int number = 2;
  66.  
  67.     while (count < points)
  68.     {
  69.         if (isPalendrome(number) && isPrime(number))
  70.         {
  71.             count++;
  72.  
  73.             if (count % PAP == 0)
  74.             {
  75.                 cout << setw(8) << number << endl;
  76.             }
  77.             else
  78.             {
  79.                 cout << setw(8) << number;
  80.             }
  81.         }
  82.         number++;
  83.     }
  84.    
  85.    
  86.  
  87. }
  88. int main()
  89. {
  90.  
  91.     cout << " prime and pal \n" << endl;
  92.     printTable(100);
  93.  
  94.  
  95.     return 0;
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement