Palindrome

Hello, Another exercise. In this particular problem we are given a five digit integer and program have to determine if its a Palindrome. Author states that we have to separate each digit into its own variables using % and /. I came up with solution but i don't know if its acceptable. It produces correct result but it looks bad to me. Here's my code, can someone look over it and give me any suggestions what i should have done instead or how i can improve it. Also author only had five digit numbers as examples.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
//deitel chaper  determine if number is Palindrome
	

#include <iostream>

using namespace std;

int main(){
	
	int iCouner, iNumOne = 0, iNumTwo = 0, iWhole,
		iRemainder, iNumThree =0, iNumFour = 0, 
		iNumFive = 0, iRev = 0;

		iWhole = 12321;

		for( iCouner = 10000; iCouner >= 1; iCouner /= 10){
		
			iRemainder = iWhole % iCouner;

			if ( ( iWhole / 10 ) > 0 ) {
				iWhole /= iCouner; 
			}

			//each variable to contain just a whole number
			
			if ( iRev == 0 )
				iNumOne = iWhole;
			else if ( iRev == 1 )
				iNumTwo = iWhole;
			else if ( iRev == 2)
				iNumThree = iWhole;
			else if ( iRev == 3 )
				iNumFour = iWhole;
			else
				iNumFive = iWhole;

			
			iWhole = iRemainder; 
			++iRev;
			
		}
		if ( iNumOne == iNumFive && iNumTwo == iNumFour )
			cout << "its a palindrome" << endl;
		else
			cout << "Its not a pslindrome" << endl;


	return 0;
}


Edit: Thanks.
Last edited on
Does your book mention functions and use strings yet? Personally I've done this problem before and have made a solution that can solve any number (as long as it fits into an integer).

Your program relies on the fact that you have a 5 digit number and you made variables for each one. One change would be to allow numbers smaller and larger than 5 digits using a vector.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <vector>
#include <iostream>

int main()
{
   int original = 12312;
   int tempDigit;
   std::vector<short> digits; // a variable length array of digits of the number;
   bool isPalindrome = true;

   if (original == 0) digits.push_back(0); // Supplied case because I use 0 as a stop in the while loop below.
   while (original != 0) // While original != 0 (Assuming positive number!)
   {
     tempDigit = original % 10; // Get the digit on the right.
     original /= 10; // Make the number smaller by one digit.
     digits.push_back (tempDigit); // add tempDigit to vector.
     // Repeat until number becomes 0;
   }

   // Now this puts out value in reverse (123 is stored in the vector as 321) so we need to
   // reverse our vector using its constructor!

   digits = std::vector<short>(digits.rbegin(), digits.rend());
   // rbegin() and rend() are iterators.

   // Now we can begin our check! You can use a for loop!
   for (int i = 0, j = digits.size() - 1; i < j; ++i, --j)
      if (digits[i] != digits[j]) isPalindrome = false;

   if (isPalindrome) std::cout << "Correct" << std::endl;
   else		     std::cout << "Failure" << std::endl;


   return 0;
}

Last edited on
This chapter covers only structures as Loops, switch , if statements. Book covers other topics later on.
Unfortunately with that limit on knowledge for the chapter you need to do heavy editing each time to the code to handle different sized numbers beyond 5 digits. But if this is any consolation I took your code and changed what may help you think it looks better. You can see I used a while loop instead of a for loop and only use 10 as a divisor the entire loop.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <iostream>

using namespace std;

int main()
{
  int iWhole = 15451,
      iNum1 = 0,
      iNum2 = 0,
      iNum3 = 0,
      iNum4 = 0,
      iNum5 = 0,
      iRemainder;

  if (iWhole == 0) cout << "Error" << endl; // Case for later.

  int i = 4; // This is for your statements below.
  while (iWhole != 0) // Use a while loop instead.
  {
    iRemainder = iWhole % 10;
    iWhole /= 10; // Now I only need to divide and mod by 10.

    // This process is now done in reverse.
    // (iNum5 is replaced then 4 and so on.)
    if ( i == 0 )
      iNum1 = iRemainder;
    else if ( i == 1 )
      iNum2 = iRemainder;
    else if ( i == 2)
      iNum3 = iRemainder;
    else if ( i == 3 )
      iNum4 = iRemainder;
    else
      iNum5 = iRemainder;

    --i;
  }

  if ( iNum1 == iNum5 && iNum2 == iNum4 )
    cout << "Its a palindrome" << endl;
  else
    cout << "Its not a pslindrome" << endl;

	return 0;
}

I would prefer a solution like this:
1
2
3
4
5
6
7
int size = log10(number)+1;//gets number of digits for the size of the array
int digits[size];
for (int i = 0; i < size; i++)// each iteration of the loop gets another digit.
{
    digits[i] = number%10;
    number /= 10;
}

It is then easy to test the array as a palindrome. Just include cmath for the log10() function.

Edit: If you're turning this in, I would fix you're grammer and spelling on lines 43 and 45
Last edited on
Does that size method work for any number (integer of course) Browni? That seems very interesting.
It would only work for counting numbers. This is what you should get for some different values:

314 : 3
123456789 : 10
27.18281828459 : 2
-234 : error

I haven't tested that exact code though so it might require very slight modification. Learn about logarithms if you want to know how it works.
Last edited on
Thanks for the input , but arrays are not covered therefore violate the requirement. Wolfgagng thanks it is better with a while loop dont know why i didnt think off of it earlier. Theres another simple method just to reverse the number but that would to violate the requirement because it would not separate each digit into its own variable, I know this is not practical ways of getting results but have to do what given problem asks.
Thanks for the input , but arrays are not covered therefore violate the requirement.

Just another example of why school is stupid. Oh, well.
Topic archived. No new replies allowed.