Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • C++ Data Types
  • C++ Input/Output
  • C++ Arrays
  • C++ Pointers
  • C++ OOPs
  • C++ STL
  • C++ Interview Questions
  • C++ Programs
  • C++ Cheatsheet
  • C++ MCQ
  • C++ Projects
  • C++ Exception Handling
  • C++ Memory Management
Open In App
Next Article:
C++ Program to Check if a Given String is Palindrome or Not
Next article icon

C++ Program to Check Whether a Number is Palindrome or Not

Last Updated : 13 Jan, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report
Try it on GfG Practice
redirect icon

A palindrome number is a number that remains the same even if its digits are reversed. In this article, we will learn how to check a given number is a palindrome or not in C++.

The easiest way to check if a number is a palindrome is to simply reverse the original number, then check if both numbers are equal. Let's take an example:

C++
#include <iostream> using namespace std;  int main() {     int n=1221;     int t = n;     int rev = 0;          // Calculate reverse number rev of a given number n     while (t > 0) {         int dig = t % 10;         rev = rev * 10 + dig;         t /= 10;     }          // Compare number n with reverse number rev      if (n==rev)         cout << "Palindrome.";     else         cout << "Not Palindrome.";     return 0; } 

Output
Palindrome.

Explanation: In this program, we reverse the number n using while loop and % modulo operator and storing the reverse number into rev variable. Then compare the original number n with reverse number rev. If they are equal print Palindrome otherwise print Not Palindrome.

There are also some other methods to check for a palindrome number in C. They are as follows:

Using Two Pointers After String Conversion

In two-pointer technique, first convert the number into a string using to_string() function and then take two pointers: one pointing at the start of the string and the other at the end of the string. Compare the characters they point to while moving these pointers towards each other. If all the characters match by the time pointers meets, the number is a palindrome, otherwise it is not.

C++
#include <bits/stdc++.h> using namespace std;  int main() {     int n = 1221;          // Convert number into string     string s = to_string(n);     int l = 0;     int r = s.size()-1;          // Compare both characters      // pointed by l and r pointer     while (l<r) {         if (s[l] != s[r]) {             cout << "Not Palindrome.";             return 0;         }                // Move both pointers         // towards center.         l++, r--;     }          // All characters matched     // means number is palindrome.     cout << "Palindrome.";     return 0; } 

Output
Palindrome.

Note: We can also use to check number n is palindrome if generate a reverse string of a generated string, then check both strings are equal to check number is palindrome.

Using Stack

The approach to push all digits of a number into stack storing them in reverse order. Then compare the digits of stack with number's corresponding digits. If any mismatch occurs, the number is not palindrome, otherwise, it is palindrome.

C++
#include <bits/stdc++.h> using namespace std;  int main() {     int n = 1231;     stack<int> st;     int t = n;          // Insert each digit from the last of     // number into stack     while (t > 0) {         st.push(t % 10);         t /= 10;     }        while (!s.empty()) {                // Check stack top element is equal         // to last digit of a Number         if (st.top() != n % 10) {           cout << "Not Palindrome.";           return 0;         }                  // Remove the top digit of         // the stack         s.pop();                // Remmove last digit of n         n /= 10;     }          // All character matched means     // number is palindrome.     cout << "Palindrome.";     return 0; } 

Output
Not Palindrome.

Explanation: In this program, pushing all digits of a number into stack. Compare top element of stack s with last digit of a number n and if any mismatch happen means number n is not palindrome otherwise number is palindrome.


Next Article
C++ Program to Check if a Given String is Palindrome or Not
author
kartik
Improve
Article Tags :
  • C++ Programs
  • C++
  • C Basic Programs
Practice Tags :
  • CPP

Similar Reads

  • Recursive program to check if number is palindrome or not
    Given a number, the task is to write a recursive function that checks if the given number is a palindrome or not. Examples: Input: 121Output: yes Input: 532Output: no The approach for writing the function is to call the function recursively till the number is wholly traversed from the back. Use a te
    4 min read
  • C++ Program to Check Whether Number is Even or Odd
    A number is even if it is completely divisible by 2 and it is odd if it is not completely divisible by 2. In this article, we will learn how to check whether a number is even or odd in C++. Examples Input: n = 11Output: OddExplanation: Since 11 is not completely divisible by 2, it is an odd number.
    4 min read
  • C++ Program to Check if a Given String is Palindrome or Not
    A string is said to be palindrome if the reverse of the string is the same as the original string. In this article, we will check whether the given string is palindrome or not in C++. Examples Input: str = "ABCDCBA"Output: "ABCDCBA" is palindromeExplanation: Reverse of the string str is "ABCDCBA". S
    4 min read
  • Program to check if an Array is Palindrome or not using STL in C++
    Given an array, the task is to determine whether an array is a palindrome or not, using STL in C++. Examples: Input: arr[] = {3, 6, 0, 6, 3} Output: Palindrome Input: arr[] = {1, 2, 3, 4, 5} Output: Not Palindrome Approach: Get the reverse of the Array using reverse() method, provided in STL.Initial
    2 min read
  • C++ Program For Checking Linked List With A Loop Is Palindrome Or Not
    Given a linked list with a loop, the task is to find whether it is palindrome or not. You are not allowed to remove the loop. Examples: Input: 1 -> 2 -> 3 -> 2 /| |/ ------- 1 Output: Palindrome Linked list is 1 2 3 2 1 which is a palindrome. Input: 1 -> 2 -> 3 -> 4 /| |/ ------- 1
    5 min read
  • C++ Program To Check If A Singly Linked List Is Palindrome
    Given a singly linked list of characters, write a function that returns true if the given list is a palindrome, else false. Recommended: Please solve it on "PRACTICE" first, before moving on to the solution. METHOD 1 (Use a Stack): A simple solution is to use a stack of list nodes. This mainly invol
    9 min read
  • C++ Program To Find Minimum Insertions To Form A Palindrome | DP-28
    Given string str, the task is to find the minimum number of characters to be inserted to convert it to a palindrome. Before we go further, let us understand with a few examples: ab: Number of insertions required is 1 i.e. babaa: Number of insertions required is 0 i.e. aaabcd: Number of insertions re
    9 min read
  • XOR and OR of all N-digit palindrome number
    Given an integer N. The task is to find the XOR and OR of all N digit palindromic numbers.Examples Input: 3 Output: XOR = 714 and OR = 1023Input: 4 Output: XOR = 4606 and OR = 16383 Approach: Find the starting and ending number of N-digit palindromic number by: starting number = pow(10, n - 1) endin
    7 min read
  • C++ Program for How to check if a given number is Fibonacci number?
    Given a number 'n', how to check if n is a Fibonacci number. First few Fibonacci numbers are 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 141, .. Examples : Input : 8 Output : Yes Input : 34 Output : Yes Input : 41 Output : No Following is an interesting property about Fibonacci numbers that can also be
    2 min read
  • Check whether it is possible to permute string such that it does not contain a palindrome of length 2
    Given a strings S length N consisting of only 'a', 'b' and 'c'. The task is to check if it is possible to permute the characters of S such that it will not contain a palindrome of length 2 or more as a substring. Examples: Input: S = "abac" Output: Yes Explanation : 1. The string contains a palindro
    5 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences