EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login

Palindrome in C++

Home » Software Development » Software Development Tutorials » C ++ Programming Tutorial » Palindrome in C++

Palindrome in C++

Introduction to Palindrome in C++

A palindrome is a number, sequence or a word that reads the same backward as forwards. Madam In Eden, I’m Adam is one of the best examples of palindrome words that sounds the same after reversing. This is where palindrome makes things interesting they act as mirrors. The name ‘palindrome’ actually means running back again according to Greek etymology. In C++ palindrome number is a number that remains the same after reverse. But how is this possible? How will we check if a number is too big and complex? Always keep in mind this small algorithm to check if a number is a palindrome or not.

  1. Get the input number from the user.
  2. Hold it in a temporary variable.
  3. Reverse the number.
  4. After reversing compare it with a temporary variable.
  5. If same then the number is a palindrome.

Don’t worry here is an example suppose we have to print palindromes between the given range of numbers. For example range is {10,122} then output should be {11, 22, 33, 44, 55, 66, 77, 88, 99, 101, 111, 121}

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

C++ program to Implement Palindrome

How to implement palindrome by using C++ program.

Code:

#include<iostream>
using namespace std;
// Function to check if a number is a palindrome or not.
int Palindrome(int n)
{
// Find reverse of n
int reverse = 0;
for (int i = n; i > 0; i /= 10)
reverse = reverse*10 + i%10;
// To check if they are same
return (n==reverse);
}
//function to prints palindrome between a minimum and maximum number
void countPalindrome(int minimum, int maximum)
{
for (int i = minimum ; i <= maximum; i++)
if (Palindrome(i))
cout << i << " ";
}
// program to test above functionality
int main()
{
countPalindrome(100,2000);
return 0;
}

Output:

implement program

Let’s take one more example specifically using a while loop that will also explain the algorithm we discussed in the introduction. We will take a number as an input from the user and check if it is a palindrome or not.

C++ program to check if a number is a palindrome or not

Let us check if a number is a palindrome or not by using C++ program.

Code:

#include <iostream>
using namespace std;
int main()
{
int n,sum=0,temp,reverse;
cout<<"Please enter the Number=";
cin>>n;
temp=n;
while(n>0)
{
reverse=n%10;
sum=(sum*10)+reverse;
n=n/10;
}
if(temp==sum)
cout<<"The number is Palindrome.";
else
cout<<"The number is not Palindrome.";
return 0;
}

Popular Course in this category
C++ Training (4 Courses, 5 Projects, 4 Quizzes)4 Online Courses | 5 Hands-on Projects | 37+ Hours | Verifiable Certificate of Completion | Lifetime Access | 4 Quizzes with Solutions
4.5 (4,890 ratings)
Course Price

View Course

Related Courses
Java Training (40 Courses, 29 Projects, 4 Quizzes)C Programming Training (3 Courses, 5 Project)

Output:

C++ output 2

C++

The above code will take a number as an input from the user and put it into a temporary variable as you can see that sum is already 0 it will use a while loop until the number becomes 0 and as the code is written it will perform the operation as written after while loop. If the number becomes 0 then it will check if the temporary variable is equal to the sum or not. If condition satisfies then it will print that the number is palindrome otherwise if condition fails it will go to else part and will print that the number is not a palindrome.

One more example using a do-while loop that will also explain the algorithm we discussed in the introduction. We will take a number as an input from the user and check if it is a palindrome or not.

C++ program to check if a number is a palindrome or not

Let us check if a number is a palindrome or not by using C++ program.

Code:

#include <iostream>
using namespace std;
int main()
{
int x, number, reverse = 0, temp ;
cout << "Please enter a number here: ";
cin >> number;
x = number;
do
{
temp = number % 10;
reverse = (reverse * 10) + temp;
number = number / 10;
} while (number != 0);
cout << " The reverse of the number is: " << reverse << endl;
if (x == reverse)
cout << " Entered number is a Palindrome.";
else
cout << " Entered number is not a Palindrome.";
return 0;
}

Output:

output 3

to check if a number is a palindrome or not

Advantages

Following are the advantages mentioned.

  • Suppose that in your project you want to match first string/element with the last one then second element/string to second last one and so on and the string will be palindrome if you reach to the middle. By just using for loop you can perform all the operations and it saves a large amount of time and space when it comes to programming because in this case, you neither have to modify the existing string nor write another variable to memory. Also, the matches required in completely equal to half of the string length.
  • If you are working on a programming language where string reversal is easy but it will require an extra amount of space to store that reverse string in another way such as recursion require more stack frame. There is one more way rather than recursion and that is writing a loop in the middle of the string to check if the corresponding letter at each end is the same or not. If unequal then break the pair early and declare the string as not a palindrome.
  • The above approach has the advantage of not wasting any computational resources such as recursion, without needing extra stack frames, but it’s also not simple as just reversing the string and checking the equality between them. It does take effort but it will always be less than other algorithms because that is the simplest way to find a palindrome.
  • Each technique has its benefits in programming and there are thousands of other ways of doing the same task but in an efficient way. It completely depends upon your current project you are working on. You only have to decide according to your situation that which technique will help you give the best benefits irrespective of the drawbacks.
  • In a real project, you need to perform n numbers of palindrome checks on a frequent basis in a short span of time then you should implement the above algorithm in the first place until and unless you require a more optimistic solution for current technical constraints.

Conclusion

By using a palindrome algorithm you can make your search more efficient and faster in finding palindromes irrespective of data types such as string character or integer. For projects that have multiple data in the different systems, these algorithms can be used to make overall performance much faster.

Recommended Articles

This is a guide to Palindrome in C++. Here we discuss the basic concept, C++ program to check and implement the Palindrome with advantages in detail. You may also look at the following article to learn more –

  1. Palindrome Program in C++
  2. Best C++ Compiler
  3. Fibonacci Series in C++
  4. Overloading in C++

C++ Training (4 Courses, 3 Projects, 4 Quizzes)

4 Online Courses

5 Hands-on Projects

37+ Hours

Verifiable Certificate of Completion

Lifetime Access

4 Quizzes with Solutions

Learn More

2 Shares
Share
Tweet
Share
Primary Sidebar
C plus plus Programming Tutorial
  • Programs
    • Patterns in C++
    • Star Patterns In c++
    • Swapping in C++
    • Reverse Number in C++
    • Palindrome Program in C++
    • Palindrome in C++
    • Factorial Program in C++
    • Fibonacci Series in C++
    • Square Root in C++
    • Random Number Generator in C++
    • Prime Number in C++
    • Leap Year Program in C++
    • Anagram in C++
    • Armstrong Number in C++
    • Reverse String in C++
    • Socket Programming in C++
    • Matrix Multiplication in C++
    • C++ using vs typedef
    • C++ vector vs list
    • C++ vector vs array
  • Basic
    • Introduction To C++
    • What is C++
    • Features of C++
    • Applications of C++
    • Best C++ Compiler
    • C++ Data Types
    • C++ Double
    • C++ unsigned int
    • User Defined Data Types in C++
    • Variables in C++
    • C++ Keywords
    • Pointers in C++
    • C++ Void Pointer
    • Function Pointer in C++
    • Iterator in C++
    • C++ Commands
    • Object in C++
    • C++ Literals
    • C++ Reference
    • C++ Undefined Reference
    • String in C++
    • C++ Programming Language (Basics)
    • C++ Identifiers
    • C++ Header Files
    • Type Casting in C++
    • C++ Formatter
  • Operators
    • C++ Operators
    • Arithmetic Operators in C++
    • Assignment Operators in C++
    • Bitwise Operators in C++
    • Relational Operators in C++
    • Boolean Operators in C++
    • Unary Operators in C++
    • C++ Operator[]
    • Operator Precedence in C++
    • C++ operator=()
  • Control Statements
    • Control Statement in C++
    • if else Statement in C++
    • Else If in C++
    • Nested if in C++
    • Continue Statement in C++
    • Break Statement in C++
    • Switch Statement in C++
    • goto Statement in C++
    • C++ Struct
    • Loops in C++
    • Do While Loop in C++
    • Nested Loop in C++
  • Functions
    • C++ String Functions
    • Math Functions in C++
    • Friend Function in C++
    • Recursive Function in C++
    • Virtual Functions in C++
    • strcat() in C++
    • swap() in C++
    • strcmp() in C++
    • ceil function in C++
    • C++ begin()
    • size() in C++
    • C++ test()
    • C++ any()
    • C++ Bitset
    • C++ find()
    • C++?Aggregation
    • C++?String append
    • C++ String Copy
    • C++ end()
    • C++ endl
    • C++ push_back
    • C++ shuffle()
    • malloc() in C++
    • C++ reserve()
    • C++ unique()
    • C++ sort()
    • C++ find_if()
    • Reflection in C++
    • C++ replace()
    • C++ search()
    • C++ Memset
    • C++ size_t
    • C++ Substring
    • C++ Max
    • C++ absolute value
    • C++ memcpy
    • C++ wchar_t
    • C++ free()
    • C++ sizeof()
    • C++ Move Semantics
  • Array
    • Arrays in C++
    • 2D Arrays in C++
    • 3D Arrays in C++
    • Multi-Dimensional Arrays in C++
    • C++ Array Functions
    • String Array in C++
    • C++ Length of Array
    • C++ arraylist
  • Constuctor and Destructor
    • Constructor and Destructor in C++
    • Constructor in C++
    • Destructor in C++
    • Copy Constructor in C++
    • Parameterized Constructor in C++
  • Overloading and overriding
    • Overloading and Overriding in C++
    • Overloading in C++
    • Overriding in C++
    • Function Overloading in C++
    • Function Overriding in C++
    • Method Overloading in C++
  • Inhertiance
    • Types of Inheritance in C++
    • Single Inheritance in C++
    • Multiple Inheritance in C++
    • Hierarchical Inheritance in C++
    • Multilevel Inheritance in C++
    • Hybrid Inheritance in C++
  • Sorting
    • Sorting in C++ 
    • Heap Sort in C++
    • C++ Vector Sort
    • Insertion Sort in C++
    • Selection Sort in C++
  • Advanced
    • C++ namespace
    • Encapsulation in C++
    • Access Modifiers in C++
    • Abstract Class in C++
    • C++ Class and Object
    • What is Template Class in C++?
    • C++ Algorithm
    • Data Structures and Algorithms C++
    • C++ Garbage Collection
    • Virtual Keyword in C++
    • Access Specifiers in C++
    • Storage Class in C++
    • Call by Value in C++
    • Multimap in C++
    • C++ Multiset
    • C++ Lambda Expressions
    • Stack in C++
    • C++ Static
    • C++ static_cast
    • Deque in C++
    • C++ Vector Functions
    • C++ 2D Vector
    • C++ List
    • C++ Mutable
    • Enum in C++
    • Abstraction in C++
    • Signal in C++
    • C++ Queue
    • Priority Queue in C++
    • Regular Expressions in C++
    • C++ Hash Table
    • File Handling in C++
    • C++ Stream
    • ifstream in C++
    • C++ ofstream
    • C++ fstream
    • C++ Read File
    • C++ iomanip
    • Macros in C++
    • Templates in C++
    • C++ setprecision
    • C++ Int to String
    • C++ thread( )
    • C++ Thread Pool
    • C++ thread_local
  • Interview question
    • C++ Interview Questions
    • Multithreading Interview Questions C++

Related Courses

C++ Training Course

Java Training Course

C Programming Course

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

Special Offer - C++ Training (4 Courses, 3 Projects, 4 Quizzes) Learn More