Ticker

6/recent/ticker-posts

Palindrome Number|Word Program in C

Palindrome Number|Word Program in C

Palindrome number: On reversing a number if the reversed number is equal to the original number then the number is called Palindrome number.

Palindrome word: On reversing a word if the reversed word is equal to the original word then the word is called Palindrome word.

Example of palindrome numbers : 11, 22, 121, 111, 12321,etc.

Example of non palindrome numbers : 12, 27, 96, 123, 12312,etc.

Example of palindrome words: mam, dad, mom, madam, etc.

Example of non-palindrome words: teacher, father, mother, madman, etc.

Algorithm:

Step 1: Accept a number/word from the user and store it in a variable say number/word.
Step 2: Reverse the number and store it in another variable say 'reverse'.
Step 3: Check if the number is equal to the 'reverse' then Display "The Given number is a Palindrome number"
else
Display "The Given number is not a Palindrome number".

Sample Input/Output:

Sample Input 1:
Enter number: 121
Sample Output 1:
The given number is a palindrome number

Sample Input 2:
Enter number: 123
Sample Output 2:
The given number is not a palindrome number

Program in C to check whether a given number is a palindrome number or not:

Code 1: This code will only work for checking Palindrome numbers and not palindrome words.


/* C Program to check whether a given number is a Palindrome Number or not */
#include <stdio.h>
int main()
{
    int numbercounter = 0;
    printf("Enter number here : ");
    scanf("%d", &number);
    int reverse = 0;
    int numberCopy = number;
    while (numberCopy > 0)
    {
        int lastDigitOfNumber = numberCopy % 10;
        reverse = reverse * 10 + lastDigitOfNumber;
        numberCopy = numberCopy / 10;
    }
    if (number == reverse)
        printf("Number is Palindrome");
    else
        printf("Number is not Palindrome");
    return 0;
}


Code 2: This code will work for checking both Palindrome Number and Palindrome Word.


/* C Program to check whether a given word/number is a Palindrome or not */

#include <stdio.h>

#include <string.h>

int main()

{

    char word[20];

    int ilen;

    printf("Enter number here : ");

    gets(word);

    len = strlen(word);

    char reverse[20];

    reverse[len] = '\0';

    for (i = 0i < leni++)

    {

        reverse[len - i - 1] = word[i];
    }

    if (strcmp(wordreverse) == 0)

        printf("Word is Palindrome");

    else

        printf("Word is not Palindrome");

    return 0;
}


If you have any doubts/questions related to Program, feel free to comment below. I hope that my code is understandable, if not then you can tell me in the comments so that I can give you an explanation of code. Don't forget to subscribe and share it with friends.

Post a Comment

0 Comments