Q3. Write a C program to check given number is palindrome or not ?

Also Interviewer can ask to you, write a C program to check given string is palindrome or not?

Palindromes are words, number or phrases that read the same in both directions. eg eye, 12321, madam etc

Part1 : To check given number is palindrome or not

#include <stdio.h>

int main()

{

    int num, num1, rem;

    int reverse = 0;

    printf(“Enter a number: “);

    scanf(“%d”, &num);

    num1 = num;

    /* logic to reverse the given number */

    while (num > 0)

    {

        rem = num % 10;

        reverse = reverse * 10 + rem;

        n = n / 10;

    }

    if (num1 == reverse)

    {

        printf(“Given number is a palindromic number\n”);

    }

    else

    {

        printf(“Given number is not a palindromic number\n”);

    }

    return 0;

}

 

Part2 : To check given string is palindrome or not

#include <stdio.h>

#include <string.h>

int main()

{

    char str[50];

    int i, length;

    int flag = 0;

    printf(“Enter a string:”);

    scanf(“%s”, str);

    length = strlen(str);

    for(i = 0; i < length; i++)

    {

        if(str[i] != str[length-i-1])

         {

            flag = 1;

            break;

         }

    }

    if (flag)

    {

        printf(“Given string is not a palindrome\n”);

    }

    else

    {

        printf(“Given string is a palindrome\n”);

    }

    return 0;

}

 

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.