C Programming – HackerRank Solution | Smallest and Largest Palindrome |

(HackerRank) Write a Modular C Programming code to solve Smallest and Largest Palindrome, write the value of the smallest palindrome and largest palindrome. Numbers are always displayed without leading zeros.

A positive integer is called a palindrome if its representation in the decimal system is the same when read from left to right and from right to left. For a given two positive integers L & K, write the value of the smallest palindrome and largest palindrome. Numbers are always displayed without leading zeros.

Input Format

First line contains L and K values

Constraints

Both L and K should be positive

L < K

Output Format

Display Smallest palindrome starting from L towards K

Display Largest palindrome starting from K towards L

Sample Input 0

10 50

Sample Output 0

Smallest palindrome = 11
Largest palindrome = 44

Sample Input 1

50 25

Sample Output 1

Invalid L and K

Refer : C Programming HackerRank all solutions for Loops | Arrays | strings

 

CODE:

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

int palindrome(int n);
int main() {
    int l,k,i;
    int last;
    scanf("%d%d",&l,&k);
    if(l<k)
    {
    for(i=l;i<=k;i++)
    {  
        if(palindrome(i)!=0)
        {
            printf("Smallest palindrome = %d\n",i);
            break;
        }
    }
    for(i=l;i<=k;i++)
    {
        if(palindrome(i))
        {
            last=i;
        }
    }
    printf("Largest palindrome = %d",last);
}
    else{
        printf("Invalid L and K\n");
    }
    return 0;
}
int palindrome(int n)
{
    int dig[20];
    int status=1,i=0;
    while(n!=0)
    {
        dig[i]=n%10;
        n=n/10;
        i++;
    }
    for(int j=0;j<(i/2);j++)
    {
        if(dig[j]!=dig[i-1-j])
           {
               status=0;
             break;
           }
    }
    return status;
}

OUTPUT

Congratulations, you passed the sample test case.

Click the Submit Code button to run your code against all the test cases.

Input (stdin)

10 50
Your Output (stdout)

Smallest palindrome = 11
Largest palindrome = 44
Expected Output

Smallest palindrome = 11
Largest palindrome = 44

 

Please find some more codes of Loops, Condition Statements, 1D Arrays, 2D Arrays, Strings, Pointers, Data Structures, Files, Linked lists, MISC, Solved model question papers & Hacker Rank all solutions on the below page:

Top 100+ C Programming codes – KLE Technological University

Leave a Comment

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

Welcome to FactsPrime

Sorry, We have detected that you have activated Ad-Blocker. Please Consider supporting us by disabling your Ad Blocker, It helps us in maintaining this website. To View the content, Please disable adblocker and refresh the page.

Thank You !!!