Utopper SkillUtopper Skill
  • Programming
    • Programming Examples
  • Interview Questions
    • DevOps Interview Questions
    • Android Interview Questions
  • How to
  • Tools
  • Top 10
  • Book Summaries
Reading: Palindrome Number in C
Share
Utopper SkillUtopper Skill
Search
  • Book Summaries
  • Programming Examples
  • C Programming Example
  • Interview Question
  • How to
  • Top 10
Follow US
Utopper Skill > Programming Examples > C Programming Example > Palindrome Number in C
Programming ExamplesC Programming Example

Palindrome Number in C

Utopper Skill Team
By Utopper Skill Team Last updated: March 1, 2023
Share
6 Min Read
Palindrome Number in C
SHARE
Table of Content
What is a Palindrome number?Approach to Check Palindrome Number in CPseudo code to Check Palindrome Number in CC Program to Check Palindrome NumberThe 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
TAGGED:C Program Examplespalindrome number
Share This Article
Facebook Twitter Whatsapp Whatsapp Telegram Copy Link Print
Previous Article Top 10 Powerful Countries in World Top 10 Powerful Countries in World
Next Article Fibonacci Series in C Fibonacci Series in C
Most Popular
Learn C Programming (Basic to Advanced)
Operators in C Programming
Utopper Skill Author By Utopper Skill Author
Top 10 Powerful Countries in World
Top 10 Powerful Countries in World
Utopper Skill Team By Utopper Skill Team
Learn C Programming (Basic to Advanced)
Macros in C
Utopper Skill Author By Utopper Skill Author
Learn C Programming (Basic to Advanced)
Do While Loop in C
Utopper Skill Author By Utopper Skill Author
Learn C Programming (Basic to Advanced)
Constant in C Programming
Utopper Skill Author By Utopper Skill Author

You Might Also Like

C++ Program To Print Prime Numbers From 1 To N
C Programming ExampleProgramming Examples

C++ Program To Print Prime Numbers From 1 To N

11 Min Read
C Program to Print Your Own Name
Programming ExamplesC Programming Example

C Program to Print Your Own Name with Example

2 Min Read
C Program To Convert Fahrenheit To Celsius
C Programming ExampleProgramming Examples

C Program To Convert Fahrenheit To Celsius

5 Min Read
C Program To Find LCM of Two Numbers
Programming ExamplesC Programming Example

C Program To Find LCM of Two Numbers

6 Min Read

Mail : [email protected]

Privacy Policy | DISCLAIMER | Contact Us

Learn

  • learn HTML
  • learn CSS
  • learn JavaScript

Examples

  • C Examples
  • C++ Examples
  • Java Examples

Study Material

  • Interview Questions
  • How to
  • Hosting
  • SEO
  • Blogging

 

© 2024 : UTOPPER.COM | Owned By : GROWTH EDUCATION SOLUTIONS PRIVATE LIMITED
Welcome Back!

Sign in to your account

Lost your password?