C Programs | IT Developer
IT Developer

C Programs



Share with a Friend

Functions in C

Palindrome check using function

C Program: Palindrome check using function

C

#include <stdio.h>

 

// Function declaration

int isPalindrome(int num);

 

int main() {

    int number;

 

    // Input number

    printf("Enter a number: ");

    scanf("%d", &number);

 

    // Function call and output

    if (isPalindrome(number))

        printf("%d is a palindrome number.\n", number);

    else

        printf("%d is not a palindrome number.\n", number);

 

    return 0;

}

 

// Function definition

int isPalindrome(int num) {

    int original = num;

    int reversed = 0, remainder;

 

    // Reverse the number

    while (num > 0) {

        remainder = num % 10;

        reversed = reversed * 10 + remainder;

        num /= 10;

    }

 

    // Compare reversed number with the original

    if (original == reversed)

        return 1;  // Palindrome

    else

        return 0;  // Not palindrome

}

Output

 
OUTPUT 1 :
Enter a number: 121
121 is a palindrome number.

OUTPUT 2 :
Enter a number: 123
123 is not a palindrome number.

Explanation

  1. Function isPalindrome(int num)
    • Stores the original number in original.
    • Reverses the number using a loop.
    • Compares the reversed number with the original.
    • Returns 1 if palindrome, 0
  2. Main Function
    • Takes input from the user.
    • Calls isPalindrome() and prints the result.