CS 111 – Programming Fundamentals
Sameer Akram Mirza
CS 111 – Programming Fundamentals
Lecture 14
Ex 6-5: Palindrome Function Program
Sameer Akram
PF – Lecture 14
Ex 6-5: Palindrome Function Program
Sameer Akram Mirza
Ex 6-5: Palindrome Function
In this example, a function, isNumPalindrome(), is
designed that returns true if a nonnegative integer is a
palindrome and returns false otherwise.
A nonnegative integer is a palindrome if it reads forward and
backward in the same way. For example, the integers 5, 44,
434, 1881, and 789656987 are all palindromes.
Ex 6-5: Palindrome Function Algorithm
Suppose num is a nonnegative integer. if(num<10), it is a
palindrome, so the function should return true.
Suppose num>=10. To determine whether num is a
palindrome, first compare the first and the last digits of num.
If the first and the last digits of num are not the same, it is not
a palindrome, so the function should return false.
If the first and the last digits of num are the same, remove the
first and last digits of num and repeat this process on the new
number, which is obtained from num after removing the first
and last digits of num.
Repeat this process as long as the num is >=10.
Ex 6-5: Palindrome Function Example
For example, suppose that the input is 18281.
Because the first and last digits of 18281 are the same,
remove the first and last digits to get the number 828.
Repeat this process of comparing the first and last digits on
828. Once again, the first and last digits are the same. After
removing the first and last digits of 828, the resulting number
is 2, which is less than 10. Thus, 18281 is a palindrome.
Ex 6-5: Palindrome Function
To remove the first and last digits of num, you first need to find
the highest power of 10 that divides num and call it pwr. The
highest power of 10 that divides 18281 is 4, that is,
pwr
pwr=4. Now 18281%10 =8281, so the first digit is
removed.
Also, because 8281/10=828, the last digit is removed.
Therefore, to remove the first digit, you can use the mod
pwr
operator, in which the divisor is 10 . To remove the last
digit, divide the num by 10. You then decrement pwr by 2 for
the next iteration.
Ex 6-5: Palindrome Function (Contd.)
Algorithm (Pseudocode)
Ex 6-5: Palindrome Algorithm
Ex6-5 PalindromeFunctionProgram
Ex6-5 PalindromeFunctionProgram
Ex6-5 PalindromeFunctionProgram
#include <iostream>
#include <cmath>
using namespace std;
bool isNumPalindrome(int); //function prototype
int main()
{
int x;
cout << "Enter non-negative integer: ";
cin >> x;
if ( isNumPalindrome( x ) == true )
cout << "The number " << x << " is Palindrome!";
else if ( isNumPalindrome( x ) == false )
cout << "The number " << x << " is not a Palindrome!";
cout << endl;
system ( "pause" );
return 0;
}
Ex6-5 Palindrome Function Definition …
//function definition
bool isNumPalindrome(int num)
{
int pwr = 0;
if (num < 10) //Step 1
return true;
Ex6-5 Palindrome Function Definition (Contd.)
else //Step 2
{
while(num/static_cast<int>(pow(10.0, pwr)) >= 10)
pwr++;
while( num >= 10 ) //Step 2.b
{
int tenTopwr = static_cast<int>(pow(10.0, pwr));
if((num/tenTopwr) != (num%10)) //Step 2.b.1
return false;
else //Step 2.b.2
{
num = num % tenTopwr;
num = num / 10;
pwr = pwr - 2;
} //end inner else
} //end while
return true;
} //end else
}
That’s end of the presentation!
12