Please login

Prime

Prepinsta Prime

Video courses for company/skill based Preparation

(Check all courses)
Get Prime Video
Prime

Prepinsta Prime

Purchase mock tests for company/skill building

(Check all mocks)
Get Prime mock

C program for longest palindrome in an array

Longest palindrome in an array.

Here we find the longest palindrome number from the given array. So here we need to sort the array in ascending order and later check from the last that the given number is palindrome or not.

The reason behind to sort an array is, we want the longest palindrome number so, in the sorted array, we got the largest number from the last element of the array, and check this is a palindrome or not and check till starting element. If there is no palindrome number so return -1 and print that there is no palindrome number.

Example

Input a[] = {1, 232, 54545, 999991};

Output: 54545

 

Longest palindrome in an array

Algorithm

Step 1. Input the size of array

Step 2. Input the elements of the array

Step 3. Sort the elements is ascending order.

Step 4. Check each element from the back weather it’s a palindrome or not.

Step 5.  Print the longest palindrome.

C Code

#include <stdio.h>  
int main()  
{
    int n, i , j, temp, rem, num;
    printf("enter size : ");
    scanf("%d",&n);
    int arr1[n];
    printf("enter elements of array : ");
    for(i=0; i<n; i++)
    {
        scanf("%d",&arr1[i]);
    }
    //sorting of array in ascending order
    for(i=0; i<n; i++)
    {
        for(j=i+1; j<n; j++)
        {
           if(arr1[i]>arr1[j])
            {
                temp = arr1[i];
                arr1[i] = arr1[j];
                arr1[j] = temp;
            }
        }
    }
    for(i=n; i>=0; i--)
    {
        num = 0;
        temp = arr1[i];
        while(arr1[i]!=0)
        {
            rem = arr1[i] % 10;
            num = num*10 + rem;
            arr1[i] = arr1[i]/10;
        }
        if(num==temp)
        {
            printf("Longest palindrome present is %d",num);
            break;
        } 
    }   
    if(i==-1)
    {
            printf("There is no palindrom number");
    }
    return 0;
OUTPUT-
CASE-1
ENTER SIZE-5
ENTER ELEMENT-
1
33
121
1111
12121
LONGEST PALINDROME IS 12121

CASE-2
ENTER SIZE-5
ENTER ELEMENTS-
31
24
32
69
79
There is no palindrome number