Palindrome number using C++

0

This C++ program checks whether the given number is a palindrome or not.





What is a Palindrome Number ?
A palindrome is a number that remains the same when its digits are reversed.



Here is how the program works:

  • The program first declares four integer variables: n, org, rev, and temp.
  • n is the input number entered by the user.
  • org is a copy of the input number, which will be used to compare with the reversed number.
  • rev is initially set to 0 and will hold the reverse of the input number.
  • The program then prompts the user to enter a number and reads the input using cin.
  • The program stores the input number n in org.
  • The program enters a while loop that continues as long as n is greater than 0. In each iteration of the loop:
  • The program calculates the last digit of n by taking its remainder when divided by 10 (n%10).
  • The program appends this last digit to rev by multiplying rev by 10 and adding the last digit (rev*10+n%10).
  • The program removes the last digit from n by dividing n by 10 (n=n/10).
  • Once the while loop has finished, the program checks whether the reversed number (rev) is equal to the original number (org).
  • If they are equal, the number is a palindrome, and the program prints "Palindrome". Otherwise, the program prints "Not a Palindrome".
  • The program returns 0 to indicate successful execution.
  • Here is an example of how the program works:
  • Copy code
  • Enter a Number : 12321 
  • Given Number:12321 
  • Reverse of Given Number:12321 
  • Palindrome
  • In this example, the user enters the number 12321.The program then calculates the reverse of this number, which is also 12321. 
  • Since the reverse and the original number are the same, the program prints "Palindrome".




Code :

#include<iostream>
using namespace std;
int main()
{
  int n,org,rev=0;
  cout<<"Enter a Number :"<<endl;
  cin>>n;
  org=n;
  while (n>0)
  {
    rev=(rev*10)+n%10;
    n=n/10;
  }
  cout<<"Given Number:"<<org<<endl;
  cout<<"Reverse of Given Number:"<<rev<<endl;
  if(rev==org)
  {
    cout<<"Palindrome"<<endl;
  }
  else{
    cout<<"Not a Palindrome"<<endl;
  }
  return 0;  
}




Conclusion:

In conclusion, we have learned how to create a palindrome program using C++. A palindrome is a word, phrase, number, or other sequence of characters that reads the same forward and backward. In this program, we first take input from the user and store it in a string variable.Then we check whether the string is a palindrome or not by comparing the characters from the beginning and the end of the string.
We have used a for loop to iterate through the string and compare the characters.If the characters are the same, we continue iterating, but if they are not the same, we exit the loop and print that the string is not a palindrome.However, if we successfully iterate through the entire string without exiting the loop,we can conclude that the string is a palindrome and print that to the user.
Overall, this palindrome program is a great exercise for practicing string manipulation and conditional statements in C++.It also demonstrates the importance of breaking down a problem into smaller, more manageable tasks and iterating through data to reach a conclusion.







Here is a simple python program of Palindrome Number.

Just copy the code and paste it in your preferred Code Editor.

if you have any Doubt Feel Free to comment down,Our team will Respond as soon as Possible.

Thanks for Reading,

Regards,

Team Dekoders.


Post a Comment

0Comments
Post a Comment (0)