Skip to content

Instantly share code, notes, and snippets.

@kylethedeveloper
Created April 9, 2018 18:42
Show Gist options
  • Save kylethedeveloper/cca5d7c5e174c744271a8165d7130fb1 to your computer and use it in GitHub Desktop.
Save kylethedeveloper/cca5d7c5e174c744271a8165d7130fb1 to your computer and use it in GitHub Desktop.
Project Euler - Problem 4 - Largest palindrome product
/*
Project_4.cpp : Largest palindrome product
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.
*/
#include <iostream>
using namespace std;
int main()
{
int num, digit, n, largest = 0, i1,j1;
//num is the multiplication, n is backup of num, digit is every digit of num,
//largest is result, i1 and j1 are numbers that are multiplied.
for (int i = 100; i <= 999; i++) // all 3 digit numbers (i1)
{
for (int j = 100; j <= 999; j++) // all 3 digit numbers (j1)
{
int reverse = 0; // reversed number
num = i * j;
n = num;
while (num != 0)
{
digit = num % 10;
reverse = (reverse * 10) + digit;
num /= 10;
}
if (n == reverse && n > largest) // "n > largest" is necessary. Delete and check the both results to see why.
{
i1 = i;
j1 = j;
largest = n;
}
}
}
cout << "Largest palindromic number: " << i1 << " x " << j1 << " = " << largest << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment