We are back again with another c++ program. In this article, we will discuss the palindrome program in c++. We will write the program using loops as well as recursion. As it is already clear, looping and recursion are very very important concepts of programming. That's why we will write the program in c++ to practice loops and recursion. Now let's begin the tutorial.
What is a palindrome number?
If we reverse the digits of a number but it still remains the same then such numbers are called palindrome numbers. In simple words, an integer number equal to its reverse is a palindrome number. for example, 151, 1221, and 48984
are palindrome numbers because they are equal to their reverse. Now let's use this knowledge to build a palindrome program in c++.
How to build a Palindrome program in c++ using recursion?
- Write declaration and definition of a function called "ReverseNum". It takes a number as a parameter.
- Declare two static variables "remainder" and "reverse" inside "ReverseNum". It's because we need this data till the end of the program. And the lifetime of the static variables is till the lifetime of the program. Static variables are initialized to zero by default.
int ReverseNum(int a) { static int remainder;
static int reverse; }
- Now if the number "a" is greater than zero, save the remainder in a remainder variable by taking modulas with 10. This step is helpful to separate the last digit and store it in the remainder variable.
- First, multiply 10 with the value of "reverse" and then add the remainder to it. This step will reverse the number.
- Call the "ReverseNum" function inside its own body with a/10 as an argument. This is called recursion. Dividing the number "a" with 10 is helpful to remove the last digit.
- But if the number "a" is not greater than 0, then simply return the reverse variable. It means we have reversed the number successfully.
- Now come to the main function. Ask the user to enter a number and take input in variable "Val".
- Call the "ReverseNum" function and store its result inside a variable called reverse.
- Display the reversed number to the user using the cout statement.
- Now apply a check using the ternary operator to see whether val and reversed num are equal or not.
- If they are equal print a message "it is a palindrome".
- Otherwise, print that it's not a palindrome.
int ReverseNum(int a) { static int remainder;
static int reverse; if (a > 0) { remainder= a % 10;
reverse = reverse * 10 + remainder;
ReverseNum(a / 10); } else { return reverse; } }
int main() { int val; int reverse; cout << "Enter a positive number:\t"; cin >> val; reverse = ReverseNum(val); return 0; }
int main() { int val; int reverse; cout << "Enter a positive number:\t"; cin >> val; reverse = ReverseNum(val); cout << "Reverse of the number:\t" << reverse << "\n\n"; string palindrome = (reverse == val) ? "It is palindrome" : "Not a palindrome"; cout << palindrome; return 0; }
Also read: Loops in C++ (for, while, and do-while).
Also read: BMI calculator program in c++.
C++ program to check palindrome number:
Now I am sharing the code of all the steps at once. I hope it will help you to understand the problem.
#include <iostream> using namespace std; int ReverseNum(int a) { static int remainder; static int reverse; if (a > 0) {
remainder = a % 10; reverse = reverse * 10 + remainder;
ReverseNum(a / 10); } else { return reverse; } } int main() { int val; cout << "Enter a positive number:\t"; cin >> val; int reverse = ReverseNum(val);
cout << "Reverse of the number:\t" << reverse << "\n\n"; string palindrome = (reverse == val) ? "It is palindrome" : "Not a palindrome"; cout << palindrome; return 0; }
Output of the program:
How to build a Palindrome program in c++ using function?
Now let's build the same program using a simple function without recursion. So, we will use a while loop for iteration and finding the reverse number and palindrome. Building different variation of the same problem helps us a lot to understand new concepts and practice our existing understanding.
- There will be a little bit of change in the "ReverseNum" function. We will use an iterative approach this time instead of recursion.
- We will use a while loop that will iterate until the number a is equal to 0. Inside the while loop, we will write our code to reverse the number.
- Once the loop is completed, the function will return the reversed number.
Check the code of the palindrome program in c++ using a function.
#include <iostream> using namespace std; int ReverseNum(int a) { int remainder; int reverse = 0; while (a != 0) { // seperate the last digit remainder = a % 10; // store last digit to reverse number reverse = reverse * 10 + remainder; // remove last digit for next iteration a = a / 10; } return reverse; } int main() { int val; int reverse; cout << "Enter a positive number:\t"; cin >> val; reverse = ReverseNum(val); cout << "Reverse of the number:\t" << reverse << "\n\n"; string plindrome = (reverse == val) ? "It is plindrome" : "Not a plindrome"; cout << plindrome; return 0; }
Output of the program:
That's all about the palindrome program in c++. I hope you liked the post. thanks for reading and supporting us.
0 Comments