Advertisement
Guest User

Untitled

a guest
Sep 10th, 2013
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.90 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4.  
  5. /* A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99.
  6.  
  7. Find the largest palindrome made from the product of two 3-digit numbers.
  8. */
  9.  
  10. int main()
  11. {
  12.  
  13.     int num = 998001;  /* num to count down from, j is the first 3 numbers and k is the next 3 */
  14.     int j = 998;
  15.     int k = 001;
  16.  
  17. while (num > 111111){
  18.  
  19. if ( (j/100) == (k%10)) {  /* this compares the first number of j and the last of k, then the opposite, and then  the middle numbers */
  20.     if (k/100 == j%10) {
  21.         if ((j%100)/10 == (k%100)/10){
  22.     printf("%d", num);
  23.     return 0;
  24.         }
  25.     }
  26. }
  27.  
  28.  
  29.  
  30. num = num - 999; /* decrement by 999 to brute force every possible answer */
  31.  
  32. j = num/1000;   /* assigns the numbers new first 3 and last 3 to j and k */
  33. k = num - (j * 1000);
  34.  
  35. }
  36.  
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement