blob: aad741a8d01326f91de00154e430e109756e03c4 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
|
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int isPalindrome(long num){
long len = 0;
long n = num;
while(n != 0){
n = n / 10;
len++;
}
char *numchar = (char *) malloc (len * sizeof(char));
sprintf(numchar, "%ld", num);
//printf("%s\n", numchar);
// Backwards counter
long j = len - 1;
// Forwards counter
long i = 0;
while(i < len){
if(numchar[i] != numchar[j])
return 0;
i++;
j--;
}
return 1;
}
long *findProduct(long num){
for(long i = 999; i > 100; i--)
for(long j = 999; j > 100; j--)
if(i * j == num){
long *facts = (long *) malloc (2 * sizeof(long));
facts[0] = i;
facts[1] = j;
return facts;
}
}
long findMaxPalindrome(){
long large = 0;
for(long i = 999; i > 100; i--){
for(long j = 999; j > 100; j--){
long test = i * j;
if(isPalindrome(test) && test > large){
large = test;
}
}
}
return large;
}
int main(){
long answer = findMaxPalindrome();
printf("%d\n", answer);
long *prod = findProduct(answer);
printf("The factors are %d and %d\n", prod[0], prod[1]);
}
|