 Project Euler, Problem #4, C

Dec 2nd, 2021 (edited)
170
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. #include <stdio.h>
2.
3. /* The task. A palindromic number reads the same both ways. The
4.  * largest palindrome made from the product of two 2-digit numbers is
5.  * 9009 = 91 × 99. Find the largest palindrome made from the product
6.  * of two 3-digit numbers. */
7.
8. int reverse(int number);
9.
10. int main()
11. {
12.      int i, j, product;
13.      int accumulator = 0;
14.
15.      for(i = 999; i > 99; i--)
16.           for(j = 999; j > 99; j--)
17.           {
18.                product = i * j;
19.                if(product == reverse(product))
20.                     if(accumulator < product)
21.                          accumulator = product;
22.           }
23.
24.      printf("The number is %d.\n", accumulator);
25.      return(0);
26. }
27.
28. int reverse(int number)
29. {
30.      int divisor = 1, multiplier = 1, output = 0;
31.
32.      while(number >= divisor * 10)
33.           divisor *= 10;
34.
35.      while(number > 0 && divisor > 0)
36.      {
37.           if(number >= divisor)
38.           {
39.                output += number / divisor * multiplier;
40.                number = number % divisor;
41.           }
42.           multiplier *= 10;
43.           divisor /= 10;
44.      }
45.
46.      return(output);
47. }
48.
49. /* The number is 906609. */
50.
RAW Paste Data