Docs


Problem 4: Largest palindrome product

Problem

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

/*
 * Largest Palindrome product
 * Problem 4 <https://projecteuler.net/problem=4>
 *
 * Project Euler
 *
 * By Ankit R Gadiya
 */

#include <stdio.h>
#include <stdbool.h>

bool check_palindrome (int num);

int main (void)
{
	int largest_pal = 0;

	for (int i = 100; i < 1000; i++) {
		for (int j = 100; j < 1000; j++) {
			int temp = i * j;
			if (check_palindrome (temp) && temp > largest_pal)
			largest_pal = temp;
		}
	}
	printf("Largest Palindrome: %i\n", largest_pal);

	return 0;
}

bool check_palindrome (int num)
{
	int reverse_num = 0, temp = num;

	while (temp > 0) {
		reverse_num = (reverse_num * 10) + (temp % 10);
		temp /= 10;
	}

	if (num == reverse_num)
		return true;
	else
		return false;
}

prob4.c

Result

906609