DEV Community

Discussion on: Project Euler #4 - Largest Palindrome Product

Collapse
 
mamounbs profile image
Mamoun Boussida • Edited

Here is my code on C (Brute force), the result was out after 0.172s.

#include <stdio.h>
#include <stdlib.h>

int palindrome(long x)
{
    int i,j=0;
    char ch[7];
    sprintf(ch,"%ld",x);

    char ch1[7]="";
    for(i=strlen(ch)-1;i>=0;i--,j++)
    {
        ch1[j]=ch[i];
    }

    if(strcmp(ch,ch1)==0)
        return 1;
    return 0;
}


int main()
{
    int i,j;
    long s,max=100*100;
    for(i=100;i<=999;i++)
    {
        for(j=100;j<=999;j++)
        {
            s=i*j;
            if (palindrome(s) && s>max)
                max=s;
        }
    }
    printf("Result = %ld",max);
}