Euler Problem 4: Largest palindrome product : C Programming Solution
Problem 4:
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.
Solution Approach:
Brute force method:
- Create a function that checks a number for being a palindrome.
- keep multiplying the numbers from 999 X 999 decretmenting one at a time.
- Find the multiplication and get the highest product and print
Solution :
#include "stdio.h"
/* 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.
*/
int isPal(int num)
{
int temp=num;
int rev=0;
int dig=0;
while(temp>0)
{
dig=temp%10;
rev=rev*10+dig;
temp/=10;
}
if(rev==num)
return 1;
else
return 0;
}
int main ()
{
int prod=1;
int large=0;
int i,j;
for( i =999;i>=100;i--)
{
for(j=999;j>=100;j--)
{
prod=i*j;
if( isPal(prod) && prod>large)
{
large=prod;
}
}
}
printf("largest palindrome prod is %d\n",large);
return 0;
}
Answer: 906609
Let me know your approach you happen to find a better way to solve this using C programming in the comment section below.
Happy coding !
