SHARE
TWEET

Untitled

a guest May 19th, 2017 43 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <algorithm>
  3. #include "euler_4.h"
  4.  
  5. /*
  6.  * A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99.
  7.  * Find the largest palindrome made from the product of two 3-digit numbers.
  8.  */
  9.  
  10. inline bool isPalindrome(int n)
  11. {
  12.     std::string a = std::to_string(n);
  13.     std::string b = a;
  14.     std::reverse(b.begin(), b.end());
  15.     return (a == b);
  16. }
  17.  
  18. void euler_4()
  19. {
  20.     int Largest = -1, Num = -1;
  21.     for (int i = 999; i >= 100; i--)
  22.     {
  23.         for (int j = i; j >= 100; j--)
  24.         {
  25.             Num = j * i;
  26.             if (isPalindrome(Num))
  27.             {
  28.                 if (Num > Largest)
  29.                 {
  30.                     Largest = Num;
  31.                 }
  32.             }
  33.         }
  34.     }
  35.  
  36.     std::cout << "The largest palindrome is " << Largest;
  37. }
RAW Paste Data
Want to get better at C++?
Learn to code C++ in 2017
Pastebin PRO Summer Special!
Get 40% OFF on Pastebin PRO accounts!
Top