
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Palindrome Integer in C++
Suppose we have a non-negative integer called num, we have to check whether it is a palindrome or not, but not using a string.
So, if the input is like 1331, then the output will be true.
To solve this, we will follow these steps −
ret := 0
x := num
while num > 0, do −
d := num mod 10
ret := ret * 10
ret := ret + d
num := num / 10
return true when x is same as ret
Let us see the following implementation to get better understanding −
Example
#include <bits/stdc++.h> using namespace std; class Solution { public: bool solve(int num) { int ret = 0; int x = num; while(num > 0){ int d = num % 10; ret *= 10; ret += d; num /= 10; } return x == ret; } }; main() { Solution ob; cout << (ob.solve(1331)); }
Input
1331
Output
1
- Related Articles
- Palindrome Partitioning in C++
- Prime Palindrome in C++
- Longest Palindrome in C++
- Shortest Palindrome in C++
- Break a Palindrome in C++
- Valid Palindrome III in C++
- Palindrome Permutation II in C++
- Palindrome Partitioning II in C++
- Largest Palindrome Product in C++
- Palindrome Partitioning III in C++
- Palindrome Substring Queries in C++
- Count all palindrome which is square of a palindrome in C++
- Double Base Palindrome in C++ Program
- Find next palindrome prime in C++
- Construct K Palindrome Strings in C++

Advertisements