Check Number Is Palindrome or Not

Beginner

Introduction

In this lab, we will learn how to check whether any number is Palindrome or not, we will write a C++ Program to check that. A palindrome number is a number that is the same after the reverse. For example, 121, 34543, 343, 131, 48984 are the palindrome numbers.

Include Headers

In this step, we will include the required header files for our program.

#include<bits/stdc++.h>
using namespace std;

Define the Function for checking Palindrome Number

In this step, we will define the function for checking whether the number is Palindrome or not.

void check_palindrome(int a){
    int temp=a;
    int res=0;
    for(int i=a;i>0;i=i/10){
        res+=res*10+i%10;
    }
    if(temp==res){
        cout<<a<<" is a Palindrome number";
    }
    else{
        cout<<a<<" is not a Palindrome number";
    }
}

Code Explanation:

We have defined a function check_palindrome that takes an integer value as a parameter.

We have defined two integer variables for holding the number. The variable temp contains the input number, and the variable res holds the reverse of the input number.

We have used a loop to reverse the number.

We have compared the temp number and res number and displayed the corresponding message.

Get Number from User and Call check_palindrome() Function

In this step, we will take the number from the user and call the check_palindrome() function to check whether the number is Palindrome or not.

int main(){
    int num;
    cout<<"Enter the number that you want to check:-";
    cin>>num;
    check_palindrome(num);
    return 0;
}

Code Explanation:

We have declared the main function.

We have defined an integer variable num for holding the user input.

We have asked the user to enter the number.

We have used the cin statement to take the user input.

We have called the check_palindrome(num) function and passed the user input num as a parameter.

Compile and Run the Program

In this step, we will compile and run the C++ program to check whether the given number is Palindrome or not. Run the following command to compile and run the C++ program:

g++ ~/project/main.cpp -o main && ./main

Complete code

Use the following code for main.cpp file:

#include<bits/stdc++.h>
using namespace std;

void check_palindrome(int a){
    int temp=a;
    int res=0;
    for(int i=a;i>0;i=i/10){
        res+=res*10+i%10;
    }
    if(temp==res){
        cout<<a<<" is a Palindrome number";
    }
    else{
        cout<<a<<" is not a Palindrome number";
    }
}

int main(){
    int num;
    cout<<"Enter the number that you want to check:-";
    cin>>num;
    check_palindrome(num);
    return 0;
}

Summary

In this Lab, we have learned how to check Number is Palindrome or Not in C++ Language. We have followed the below steps:

  • Include the necessary header files.
  • Define the function for checking Palindrome number.
  • Get the number as input from the user and call the function to check whether the number is Palindrome or Not.
  • Compile the program using the g++ command and run it in the terminal.

After following these steps, you can check any given number is Palindrome or Not in C++ Language.

Other Tutorials you may like