Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- /* The task. 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 reverse(int number);
- int main()
- {
- int i, j, product;
- int accumulator = 0;
- for(i = 999; i > 99; i--)
- for(j = 999; j > 99; j--)
- {
- product = i * j;
- if(product == reverse(product))
- if(accumulator < product)
- accumulator = product;
- }
- printf("The number is %d.\n", accumulator);
- return(0);
- }
- int reverse(int number)
- {
- int divisor = 1, multiplier = 1, output = 0;
- while(number >= divisor * 10)
- divisor *= 10;
- while(number > 0 && divisor > 0)
- {
- if(number >= divisor)
- {
- output += number / divisor * multiplier;
- number = number % divisor;
- }
- multiplier *= 10;
- divisor /= 10;
- }
- return(output);
- }
- /* The number is 906609. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement