Create a free account on Computer Sir Ki Class and unlock access to valuable learning resources.
Exam Paper Downloads: After registering and logging in, you can download exam question papers in PDF format to practice and prepare for your exams.
Student Support: Get assistance with your doubts, queries, and questions related to your studies. Our support is here to help you understand topics and resolve your learning difficulties.
Register today and make your exam preparation easier and more effective!
Problem Statement - Test palindrome for an integer number
Write a program where a function of prototype int ispalin(int) is written, which returns 1 when
given input number is a palindrome (eg. 121, 16561) else returns 0. Show implementation of this function in main routine by collecting a positive integer from the user.
Check if number is a palindrome without using modulus operator
×
Ask AI About This Code
Default Prompt that is passed along with the code is given below, You can write your own prompt if you so desire.
×
Test CPP
The code has been copied automatically for you in the clipboard. After that open any of the service and press cntrl-v to paste in the given code area. Needs a bit of tweaking as per the link.
Related codes are those codes where similar programming concepts are in use. They are different from alternate codes in the sense that they may not have same purpose and results.
:
Run Output
Enter a positive integer: 16561
palindrome
-OR-
Enter a positive integer: 1653
Not palindrome
int ispalin(int); //Prototype as given in the problem declared here
int n; cout<<“Enter a positive integer: “; cin>>n;
Collecting value from the user.
if(ispalin(n)) cout<<“palindrome”<<endl; else cout<<“Not palindrome”<<endl;
ispalin(n) directly called in the if condition as it returns 1 when palindrome else 0.
return 0;
int ispalin(int n) { int rem,nr=0; Related variables are declared, rem will contain the remainder of mod operations and nr has been used to maintain the reversed number.
int nc=n;
Since forthcoming loop of reversal of number will affect the original number so it is better to make a number copy in nc and then the loop is worked on the working copy.
while(nc>0) //This loop is to reverse the number, works till all digits are exhausted { rem=nc%10;
Last digit is extracted here
nr=nr*10+rem;
last number is shifted left by multiplying it with 10 and then new last digit is added.
nc=nc/10;
Last digit of number is dropped so that operation can continue with other digits }
Finally we are left with reversed number in the nr variable.
if(n==nr) return 1; else return 0;
A number is a palindrome when reversed number is same as given number so 1 is returned else 0 is returned. }
Notes
Negative numbers can easily be tested for palindrome by passing the absolute value to this function.
Common Errors
Student some time forget to retain the number copy in another variable and directly act on given input variable. In this case the original number value is lost after the loop is complete.