Commit 277e66e0 by Andrew Dahl

Solved Problem 4

parent 9f76244e
Showing with 51 additions and 0 deletions
Question:
A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 99.
Find the largest palindrome made from the product of two 3-digit numbers.
Answer: 906609
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
bool is_palindrome(int num)
{
bool is_pal = true;
stringstream ss;
ss << num;
string str = ss.str();
for(int i = 0; i < str.length()/2; i++)
{
if(str[i] != str[str.length()-1-i])
is_pal = false;
}
return is_pal;
}
int main() {
int largest = 0;
int largest_i = 0;
int largest_j = 0;
for(int i = 999; i > 100; i--)
{
for(int j = 999; j > 100; j--)
{
if(is_palindrome(i*j) && largest < i*j)
{
largest = i*j;
largest_i = i;
largest_j = j;
}
}
}
cout << largest_i << " * " << largest_j << " = " << largest << endl;
return 0;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment