C++ Program to check number is palindrome or not using call by Reference


In this program, You will learn how to check number is palindrome or not using call by Reference in C++.


Call by Reference

void check(int n, int *p) { 
       //statement
}

Example: How to check number is palindrome or not using call by Reference in C++.

#include<iostream>
using namespace std;

void check(int n, int *p) {

    int r, rev = 0;
    while (n > 0) {
        r = n % 10;
        rev = rev * 10 + r;
        n = n / 10;
    }
    *p = rev;
}

int main() {

    int n, p = 0;

    cout << "Enter a number:";
    cin>>n;

    check(n, &p);
    if (p == n) {
        cout << "Number is palindrome:" << n;
    } else {
        cout << "Number is not palindrome:" << n;
    }

    return 0;
}

Output:

Enter a number:121
Number is palindrome:121