Hey all..
Code:
/* A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 x 99.
*
* Find the largest palindrome made from the product of two 3-digit numbers. */
#include <stdio.h>
int main (void)
{
int reverse_num = 0, remainder, temp;
int a = 100, b = 100;
int sum = 0;
int ctr;
int large;
large = sum;
for (a = 100; a < 999; a++) // create loop for var a from 100 to 999
{
for(b = 100; b < 999; b++) // create loop for var b from 100 to 999
{
do
{
sum = a * b;
temp = sum;
while (temp != 0) // loop to check digits in temp (separate the digits)
{
remainder = temp % 10;
reverse_num = reverse_num * 10 + remainder;
temp /= 10;
}
// printf("%d", reverse_num); // with this commented out this isn't printing the correct sum out in reverse order
++ctr;
if (sum % sum == 0) // find largest palindrome number and store it in var large
{
if(sum > large)
{
large = sum;
}
}
} while (ctr < 'C'); // infinite loop
}
}
printf("%d is largest palindrome number.\n", large); // print largest palindrome number
return 0;
}
Program output:
Code:
996004 is largest palindrome number.
With
Code:
// printf("%d", reverse_num);
code comments removed, the number (sum) is not being calculated and displayed correctly in reverse and the algorithm is being copied correctly (the source code I used) i've found the modulus source code for the reverse number in the while loop on the internet a few places.
Also, is this nested for loop, correct? To try all calculations for a palindrome number. Thanks.
a = 100; a < 999; a++ ?
b = 100; b < 999; b++ ?
Problem 4 - Project Euler