Friday, November 25, 2016

Largest Palindrome


A palindromic number reads the same both ways. The smallest 6 digit palindrome made from the product of two 3-digit numbers is 

Find the largest palindrome made from the product of two 3-digit numbers which is less than .
Input Format
First line contains  that denotes the number of test cases. This is followed by  lines, each containing an integer, .
Constraints
Output Format
Print the required answer for each test case in a new line.
Sample Input 0
2
101110
800000
Sample Output 0
101101
793397
Explanation 0
  •  is product of  and .
  •  is product of  and .
NOTE: WRITE YOUR CODE IN COMMENTS SECTION

*******************
PROGRAM
*******************

#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>

int checkPalindrome(int n)
{
    int rev=0,original=n;
    while(n>0)
    {
        rev=rev*10+n%10;
        n=n/10;
    }
    if(rev==original)
        return 1;
    else
        return 0;
   
}
int main(){
    int t;
    scanf("%d",&t);
    for(int a0 = 0; a0 < t; a0++){
        int n,i,j,prod;
        scanf("%d",&n);
        int max=101101;
        for(i=100;i<=999;i++)
        {
            for(j=100;j<=999;j++)
            {
                prod=i*j;
                if(prod>max && prod <n)
                {
                    if(checkPalindrome(prod)==1)
                    {
                        max=prod;
                    }
                }
            }
               
        }
        printf("%d\n",max);
    }
    return 0;
}

Contact Form

Name

Email *

Message *