C Program to Check Palindrome Number

Introduction

A palindrome number is a number that remains the same when its digits are reversed. For example, 121 and 1331 are palindromes. This guide will show you how to write a C program to check whether a given number is a palindrome.

Problem Statement

Create a C program that:

  • Takes an integer input from the user.
  • Checks whether the number is a palindrome.
  • Displays the result.

Example:

  • Input: 121
  • Output: 121 is a palindrome

Solution Steps

  1. Include the Standard Input-Output Library: Use #include <stdio.h> to include the standard input-output library, which is necessary for using printf and scanf functions.
  2. Write the Main Function: Define the main function, which is the entry point of every C program.
  3. Declare Variables: Declare variables to store the original number, the reversed number, and the remainder.
  4. Input the Number: Use scanf to take input from the user for the number.
  5. Reverse the Number: Use a loop to reverse the digits of the number.
  6. Check for Palindrome: Compare the reversed number with the original number.
  7. Display the Result: Use printf to display whether the number is a palindrome.

C Program

#include <stdio.h>

/**
 * C Program to Check Palindrome Number
 * Author: https://www.javaguides.net/
 */
int main() {
    // Step 1: Declare variables to hold the original number, reversed number, and remainder
    int num, originalNum, reverse = 0, remainder;

    // Step 2: Prompt the user to enter a number
    printf("Enter an integer: ");
    scanf("%d", &num);

    // Step 3: Store the original number
    originalNum = num;

    // Step 4: Reverse the number using a loop
    while (num != 0) {
        remainder = num % 10;           // Get the last digit
        reverse = reverse * 10 + remainder; // Append the last digit to the reversed number
        num = num / 10;                 // Remove the last digit from the original number
    }

    // Step 5: Check if the original number is equal to the reversed number
    if (originalNum == reverse) {
        printf("%d is a palindrome.\n", originalNum);
    } else {
        printf("%d is not a palindrome.\n", originalNum);
    }

    return 0;  // Step 6: Return 0 to indicate successful execution
}

Explanation

Step 1: Declare Variables

  • The variable num is declared to store the original number entered by the user. originalNum is used to store the original number for comparison later. reverse is initialized to 0 and will hold the reversed number. remainder is used to store the last digit of the number during each iteration of the loop.

Step 2: Input the Number

  • The program prompts the user to enter an integer using printf. The scanf function then reads the input and stores it in the variable num.

Step 3: Store the Original Number

  • The original number is stored in the variable originalNum so that it can be compared later with the reversed number.

Step 4: Reverse the Number Using a Loop

  • The program uses a while loop to reverse the digits of the number:
    • Step 4.1: The remainder when num is divided by 10 gives the last digit of num.
    • Step 4.2: This last digit is then added to the reverse variable after multiplying reverse by 10 to shift its digits to the left.
    • Step 4.3: The original number num is divided by 10 to remove its last digit. This process repeats until num becomes 0.

Step 5: Check if the Number is a Palindrome

  • The program compares the original number originalNum with the reversed number reverse. If they are equal, the number is a palindrome; otherwise, it is not.

Step 6: Display the Result

  • The program displays whether the number is a palindrome using the printf function.

Step 7: Return 0

  • The return 0; statement indicates that the program executed successfully.

Output Example

Example 1:

Enter an integer: 121
121 is a palindrome.

Example 2:

Enter an integer: 123
123 is not a palindrome.

Example 3:

Enter an integer: 1331
1331 is a palindrome.

Conclusion

This C program demonstrates how to check whether a given number is a palindrome. It covers basic concepts such as loops, arithmetic operations, and conditional statements, making it a useful example for beginners learning C programming.

Leave a Comment

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

Scroll to Top