Sunday, September 11, 2011

C program : largest palindrome made by multiplication of two 3 digit numbers





/*
Definition from Aniruddha Guddi:
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.
*/


/**************************
* author: umang bhatt *
* bhatt.umang7@gmail.com *
**************************/





#include<stdio.h>

#define TRUE 0
#define FALSE 1

int
is_palindrome(int no)
{

int
temp = no;

int
ans = 0;
int
r;
while
(temp > 0)
{


ans = ((ans * 10) + (temp % 10));

temp = temp / 10;
}

if
(ans == no)
{


r = TRUE;
}

else

{

r = FALSE;
}


return
r;
}


int
main()
{

int
f_no = -1;

int
l_no = -1;
int
last_palindrom = -1;

int
flag = FALSE;
int
i, j;

for
(i = 100; i < 999; i++)
{


for
(j = 100; j < 999; j++)
{


if
(is_palindrome(i * j) == TRUE)
{

if
((i * j) > last_palindrom)
{


last_palindrom = i * j;
f_no = i;

l_no = j;
}
}
}
}

printf("%d * %d = %d \n", f_no, l_no, last_palindrom);

return
0;
}




2 comments: