Saturday, 8 August 2026

Palindrome Number

 A palindrome number is one that reads the same backward as forward, such as 121 or 1331. In this tutorial, we’ll write the palindrome number program in C using various approaches like loops, recursion, and arrays. 

Understanding how to implement a palindrome program in C language is crucial for mastering concepts like reversing numbers, handling digits, and comparing values. Such programs have real-world applications in data validation, cryptography, and algorithms requiring symmetry checks. 

By learning to solve this problem, you’ll strengthen your logical thinking and gain foundational skills for solving more advanced programming challenges.

Coders' comments

Small concepts like palindromes build great thinking, Each program adds to the foundation you’re linking.

Option 1: Palindrome Program in C Using for Loop

#include <stdio.h>

int main() {
    int number, original, reversed = 0;

    printf("Enter a number: ");
    scanf("%d", &number);

    original = number;

    for (; number != 0; number /= 10) {
        reversed = reversed * 10 + number % 10;
    }

    if (original == reversed) {
        printf("%d is a palindrome number.\n", original);
    } else {
        printf("%d is not a palindrome number.\n", original);
    }

    return 0;
}

Output

Enter a number: 1331
1331 is a palindrome number.

Explanation: 

This palindrome program in C language using for loop reverses the digits iteratively by extracting them using % and accumulating them in reversed. The result is compared with the original number to check for palindromes.

Option 2: C Program for Palindrome Number Using Recursion

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

int isPalindrome(int number, int reversed) {
    if (number == 0)
        return reversed;
    return isPalindrome(number / 10, reversed * 10 + number % 10);
}

int main() {
    int number;

    printf("Enter a number: ");
    scanf("%d", &number);

    if (number == isPalindrome(number, 0)) {
        printf("%d is a palindrome number.\n", number);
    } else {
        printf("%d is not a palindrome number.\n", number);
    }

    return 0;
}

Output

Enter a number: 45689
45689 is not a palindrome number.

Explanation: 

This simple palindrome number program in C uses recursion to reverse the number digit by digit. The reversed number is compared with the original to verify if it’s a palindrome.

Option 3: Palindrome Program in C Using while Loop

#include <stdio.h>

int main() {
    int number, original, reversed = 0, remainder;

    printf("Enter a number: ");
    scanf("%d", &number);

    original = number;

    while (number != 0) {
        remainder = number % 10;
        reversed = reversed * 10 + remainder;
        number /= 10;
    }

    if (original == reversed) {
        printf("%d is a palindrome number.\n", original);
    } else {
        printf("%d is not a palindrome number.\n", original);
    }

    return 0;
}

Output

Enter a number: 121
121 is a palindrome number.

Explanation

This C program to check number is palindrome or not uses a while loop to reverse the given number. 

The original number is stored in original. Inside the loop, the last digit is extracted using the modulus operator (%), added to reversed after multiplying by 10, and then the number is divided by 10.

After the loop, the reversed number is compared with the original. If they match, the number is a palindrome; otherwise, it’s not. This approach is efficient for integer palindrome checks.

Option 4: Palindrome Program in C Using Function

#include <stdio.h>

int reverseNumber(int number) {
    int reversed = 0;
    while (number != 0) {
        reversed = reversed * 10 + number % 10;
        number /= 10;
    }
    return reversed;
}

int main() {
    int number;

    printf("Enter a number: ");
    scanf("%d", &number);

    if (number == reverseNumber(number)) {
        printf("%d is a palindrome number.\n", number);
    } else {
        printf("%d is not a palindrome number.\n", number);
    }

    return 0;
}

Output

Enter a number: 456
456 is not a palindrome number.

Explanation: 

This C program for palindrome number modularizes the logic into reverseNumber, which computes the reverse. The main function compares the reversed value with the original to determine if it’s a palindrome.

No comments:

Post a Comment