division and modulus to determine palindrome

Started by
12 comments, last by gnomer 18 years, 8 months ago
I am fairly new to C++ programming. I've had a few classes in the past but that was a while ago so I'm pretty much starting all over. I am currently working my way through the textbook "C++ How to Program 4e". The book has exercises after each chapter which I try to do as many of as I can figure out. The problem is that most of them have no documented solution so I come to you all here for help. I am only just finishing Chapter 2: Control structures. The next chapter is functions, to give you an idea of where I am. So the solutions at this point should be pretty basic. For anyone with the book the exercise is 2.29 and it reads as follows. "A palindrome is a number or a text phrase that reads the same backwards as forwards. For example, each of the following five-digit integers is a palindrome: 12321, 55555, 45554 and 11611. Write a program that reads in a five-digit integer and determines whether it is a palindrome.(Hint: Use the division and modulus operators to separate the number into its individual digits." I just can't figure this one out. I'm probably approaching it totally wrong. I hope I am posting this in the proper forum. If not please direct me to the right place. :) Looking forward to any assistance offered. Thanks!
Advertisement
char string[256];char *end = string[strlen(string)];char *start = string;while (*(end--) == *(start++) && end != start) {}if (end != start)   // not a palindrome

That's not tested, but if it doesn't work, it'll at least give you an idea how to do it.
I think your solution is more advanced than the chapter I am on. I'm not sure when I get into pointers and arrays. I think those are pointers if I remember correctly. :)

I'm guessing the solution using the material I've covered so far would be very inefficient but so far it eludes me. I'll keep trying though.

oh and I did try your code just to see the result but I get this error:
"error C2440: 'initializing' : cannot convert from 'char' to 'char *'"

Thanks for taking a look at my problem. :)
no that don't seem appropriate.

#include <iostream>#include <algorithm>#include <sstream>#include <string>bool is_palindrome (const std::string& str){	return std::equal(str.begin(),str.end(),str.rbegin());}bool is_palindrome (const int numb){	std::stringstream a;	a << numb;	return is_palindrome(std::string(a.str()));}				int main (){	std::cout << is_palindrome(535535) << std::endl;		// this should return true	std::cout << is_palindrome("racecar") << std::endl;		// this should return true	std::cout << is_palindrome(1121146890) << std::endl;	// this should return false	std::cout << is_palindrome("ghhjjjdfi") << std::endl;	// this should return false    return 0;} 
"I turned into a driveway I thought was mine and crashed into a tree I don't have."- a real insurance claim
Easy-shmeezy[smile]

Some hints on how to break the number into it's digits:

Observe that modding a number by ten yields the right-most digit: 386 % 10 = 6; 62 % 10 = 2. Also, integer division by ten, ignoring remainder, effectively takes off the rightmost digit: 386 / 10 ~= 38; 62 / 10 ~= 6.

The rest of the problem is easy. For an n digit number, check that digits 0 and n are equal; then check 1 and n-1; then 2 and n-2; then 3 and n-3; and so on and so forth until you use up all the digits, or arrive at a single middle digit.

If all outer pairs were equivalent, then the number is a palindrome. Else, it's not.

EDIT: All you need for this is an array (in fact, you don't *need* this, but it would really clean up the code) and two loops (the kinds of which are up to you; one loop to find the digits, another to determine if it's a palindrome).

HTH,
nilkn
I'm trying something now, ill post results if this works. Thanks for the hints :)
You probably aren't aware of this, so I'll just tell you [smile]

In C++, division by integers results in automatic truncation of the results. So if the result is in actuality a decimal, C++ will just cut off that decimal part, leaving just the integer portion.

For example:
// Continuing the above examples:int operand1 = 386;int operand2 = 10;int result = 386 / 10;// Of course, the *actual* quotient is 38.6, but since// all three operands are of type 'int', C++ automatically// truncates the 6 off. Thus, result == 38.
ok here is what i came up with. very basic and most likely severely inefficient given the topics covered so far in my book... But, it seems to work :P

#include <iostream>using std::cout;using std::cin;using std::endl;int main(){	int number;	int n1, n2, n3, n4, n5;	bool isPalindrome = true;		cout << "Enter a 5 digit number: " << endl;	cin >> number;	n1 = number / 10000;	cout << "First digit: " << n1 << endl;	n2 = (number % 10000) / 1000;	cout << "Second digit: " << n2 << endl;	n3 = (number % 1000) / 100;	cout << "Third digit: " << n3 << endl;	n4 = (number % 100) / 10;	cout << "Fourth digit: " << n4 << endl;	n5 = number % 10;	cout << "Fifth digit: " << n5 << endl;	if( n1 != n5 )		isPalindrome = false;	if( n2 != n4 )		isPalindrome = false;	if(isPalindrome)		cout << "The number is a palindrome" << endl;	else		cout << "The number is NOT a palindrome" << endl;	return 0;}
That looks like it would do the trick. GW :)
"I turned into a driveway I thought was mine and crashed into a tree I don't have."- a real insurance claim
i couldnt get it into a loop form like you suggested...would i need an array for that? maybe im just too tired to think of a way :)

This topic is closed to new replies.

Advertisement