Skip to content
Utopper skill
  • ProgrammingExpand
    • Programming Examples
  • Interview QuestionsExpand
    • DevOps Interview Questions
    • Android Interview Questions
  • How to
  • Tools
  • Top 10
  • Book Summaries
Utopper skill
Programming Examples C Programming Example

Palindrome Number in C

Article Contents

  1. What is a Palindrome number?
  2. Approach to Check Palindrome Number in C
  3. Pseudo code to Check Palindrome Number in C
  4. C Program to Check Palindrome Number
  5. The output will look something like this:

What is a Palindrome number?

Palindrome Number in C

A palindrome number is a number that remains the same when its digits are reversed. For example, the number 121 is a palindrome because it is the same when read forwards or backward. Similarly, the number 1221 is also a palindrome.

Palindrome numbers can be integers or decimal numbers and can be of any length. They can also be negative numbers, but they are not common.

Palindrome numbers are used in many mathematical and computer science problems, such as in algorithms to check if a number is a palindrome or in coding challenges to find the largest palindrome made from the product of two three-digit numbers.

Approach to Check Palindrome Number in C

One approach to check if a number is a palindrome in C is to reverse the digits of the number and compare the reversed number with the original number. This can be done using a while loop and mathematical operations such as modulus and division.

Here is an example of this approach:

  1. Get the number from the user and store it in a variable (e.g. num).
  2. Create a variable (e.g. temp) and assign it the value of num.
  3. Create a variable (e.g. rev) and initialize it to 0.
  4. Use a while loop to iterate through the digits of temp.
  5. In each iteration, use the modulus operator (e.g. temp % 10) to get the last digit of temp.
  6. Multiply rev by 10 and add the last digit obtained from the modulus operation to rev.
  7. Divide temp by 10 to remove the last digit.
  8. Repeat steps 4-7 until temp becomes zero.
  9. After the while loop, check if rev is equal to num.
  10. If they are equal, the number is a palindrome. If not, it is not a palindrome.

This approach can also be optimized by using a string-based operation to compare the original value with the reverse of the string.

It is important to mention that this approach is not the only way to check if a number is a palindrome in C, and there may be other methods to accomplish this task.

Pseudo code to Check Palindrome Number in C

Here is some sample pseudocode that demonstrates how to check if a number is a palindrome in C:

int num, temp, digit, rev = 0;

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

temp = num;

while (temp != 0) {
    digit = temp % 10;
    rev = rev * 10 + digit;
    temp = temp / 10;
}

if (rev == num) {
    printf("%d is a palindrome number", num);
} else {
    printf("%d is not a palindrome number", num);
}

In this example, the user is prompted to enter a number, which is then stored in the variable “num.” The variable “temp” is then assigned the value of “num” and is used in the while loop to reverse the digits of the original number. The reversed number is stored in the variable “rev.” After the while loop has finished, the program checks if “rev” is equal to “num.” If they are equal, the original number is a palindrome and the program outputs “num is a palindrome number.” If they are not equal, the original number is not a palindrome and the program outputs “num is not a palindrome number.”

C Program to Check Palindrome Number

#include <stdio.h>

int main() {
    int num, temp, digit, rev = 0;

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

    temp = num;

    while (temp != 0) {
        digit = temp % 10;
        rev = rev * 10 + digit;
        temp = temp / 10;
    }

    if (rev == num) {
        printf("%d is a palindrome number", num);
    } else {
        printf("%d is not a palindrome number", num);
    }

    return 0;
}

In this program, the user is prompted to enter a number, which is then stored in the variable “num”. The variable “temp” is then assigned the value of “num” and is used in the while loop to reverse the digits of the original number. The reversed number is stored in the variable “rev”. After the while loop has finished, the program checks if “rev” is equal to “num”. If they are equal, the original number is a palindrome and the program outputs “num is a palindrome number.” If they are not equal, the original number is not a palindrome and the program outputs “num is not a palindrome number.”

In this program, the user input validation is not handled and it will only work with integers.

The output will look something like this:

Enter a number: 121
121 is a palindrome number
Enter a number: -121
-121 is a palindrome number
Post Tags: #C Program Examples#palindrome number
Avatar photo
Utopper Skill Team
Facebook X Instagram YouTube Linkedin Pinterest

Post navigation

Previous Previous
Top 10 Powerful Countries in World
NextContinue
Fibonacci Series in C

Latest Interview Questions

  • C++ Program To Print Prime Numbers From 1 To N
  • C Program to Print Your Own Name with Example
  • Book Summary of Why We Sleep By Matthew Walker
  • Book Summary of Indistractable By Nir Eyal
  • C Program To Convert Fahrenheit To Celsius
  • Book Summary of The One Thing By Gary Keller and Jay Papasan
  • Facebook
  • Instagram
  • YouTube
  • Telegram
  • LinkedIn
  • Twitter

Mail : [email protected]

Privacy Policy | DISCLAIMER | Contact Us

Learn Development

learn HTML

learn CSS

learn JavaScript

Examples

C Examples

C++ Examples

Java Examples

Study Material

Interview Questions

How to

Hosting

SEO

Blogging

© 2023 Utopper.com

All Rights Reserved with Copyright & Registered TradeMarks
OWNED BY : GROWTH EDUCATION SOLUTIONS PRIVATE LIMITED

Scroll to top
  • Programming
    • Programming Examples
  • Interview Questions
    • DevOps Interview Questions
    • Android Interview Questions
  • How to
  • Tools
  • Top 10
  • Book Summaries
Search