Check wether a number is palindromic!

Hi everybody!

I am working on a program, that tells wether a number is palindromic, and in my opinion, the code i have pasted works perfectly for such purpose. However, when I submit it for an online correction, it seems that for some number(s) it doesen't work.

Note: The number will always be natural.

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
#include <iostream>
#include <vector>
using namespace std;

bool is_palindromic (int n) {
	bool b = false;
	//vector to store the digits of n in a reversed way
	vector<int> coefs;
	//store the value of n
	int z = n; 
	//put the digits of n in the vector in a reversed order
	while (n != 0) {
		coefs.push_back(n%10);
		n = n/10;
	}
	//check wether the vector matches the number
	int t;
	int a = coefs.size();
	for (int i = 0; i < a; ++i) {
		if (coefs[a-1-i] == z%10) ++t;
		z = z/10;
	}
	if (t == a) b = true;
	return b;
}

int main () {
	int n;
	while (cin >> n) {
		if (is_palindromic(n)) cout << "YES";
		else cout << "NO";
		cout << endl;
	}
}


Brief explanation:

Once the number has been entered, i use the command push_back to store each digit of the number in a vector in a reversed order as well as to get the size of the digit by increasing a variable by 1 each time the while loop is executed.

Once the number is in the vector, I use a for to check whether the digits of the vector (starting from the end of the vector, which is the beginning of the number) match the digits from the number. If this happens, a counter is increased. At the end, if the counter equals the size of the vector it means that all the numbers were the same, so the value returned is true.

I certainly don't know why it doesen't work, I hope that someone can help me.

Thanks to anyone that took the time to read the post, I appreciate it!

Have a nice day!
Last edited on
Have you used iterators before?

I would do something like this
http://ideone.com/jedk7x
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
#include <iostream>
#include <vector>
#include <iomanip>

bool is_palindrome( int value )
{
	std::vector<int> digits;
	
	while( value != 0 )
	{
		digits.push_back( value % 10 );
		value /= 10;
	}
	
	//instead of std::vector<int>::iterator... you can use auto
	std::vector<int>::const_iterator it_begin = digits.cbegin(); //you don't have to use const
	std::vector<int>::const_reverse_iterator it_end = digits.crbegin(); //you don't have to use const
	
	while( it_begin != digits.cend() && it_end != digits.crend()  )
	{
		if( *it_begin != *it_end ) return( false );
		++it_begin;
		++it_end;
	}
	return( true );
}

int main()
{
	std::cout << std::boolalpha;
	int input = 0;
	do
	{
		std::cout << "Please enter a number(-999 to quit): ";
		std::cin >> input;
		std::cout << "Is " << input << " a palindrome? " << is_palindrome( input ) << std::endl;
	} while( input != -999 );
	
	return( 0 );
}


or even

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
#include <iostream>
#include <vector>
#include <iomanip>

bool is_palindrome( int value )
{
	std::vector<int> digits;
	
	while( value != 0 )
	{
		digits.push_back( value % 10 );
		value /= 10;
	}
	
	//instead of std::vector<int>::iterator... you can use auto
	std::vector<int>::const_iterator it_begin = digits.cbegin(); //you don't have to use const
	std::vector<int>::const_reverse_iterator it_end = digits.crbegin(); //you don't have to use const
	
	while( it_begin != digits.cend() && it_end != digits.crend()  )
	{
		if( *it_begin != *it_end ) return( false );
		++it_begin;
		++it_end;
	}
	return( true );
}

int main()
{
	std::cout << std::boolalpha;
	int input = 0;
	while( std::cout << "Please enter a number(-999 to quit): " &&
		std::cin >> input )
	{
		std::cout << "Is " << input << " a palindrome? " << is_palindrome( input ) << std::endl;
	}
	
	return( 0 );
}
1
2
3
4
5
6
7
8
9
10
11
12
bool is_palidrome( unsigned int number )
{
    unsigned int value = 0 ;

    for( unsigned int n = number ; n > 0 ; n /= 10 )
    {
        value *= 10 ;
        value += n % 10 ;
    }

    return value == number ;
}
I am quite a beginner in this, so I dont really know what an interator or unsigned value are though i hear it a lot...:p
unsigned just means it is only positive numbers basically say you have an 4 byte number
that means it is 32 bits in a normal number ( signed ) it has a sign bit ( 0 bit )
so it only has 31 bits for numbers.
each bit holds a 1 or a 0 and it stores a binary value
So basically a 31 bit means
20 -> 230 so it can go from - 2147483647 -> +2147483647

basically in binary each extra bit means you have 2 * the previous - 1 ( first bit starts at value of 1 not 2 )

so if we have an unsigned it doesnt have that first bit as a sign so it has 32 bits of numbers

therefore it is twice as many positive digits

0 -> +4294967295 ( 231 - 1 )

each byte contains 8 bits probably should of mentioned earlier.

iterators are pretty much pointers.
They are useful for iterating ( looping ) through arrays/vectors
Nice! Tried it and worked!

Thanks a lot to everyone!
On the subject of iterators, I will also add using string iterators. Why bother with using numbers at all? Instead use a string to get the number:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <string>
#include <iostream>

using std::cin;
using std::cout;
using std::string;

typedef string::iterator f_iter;

string Palindrome(string &number) {
	f_iter t_start = number.begin(), f_start = number.end();	
	while (t_start < --f_start)
		if ( *t_start++ != *f_start ) return "NO";
	return "YES";
}

int main() {
	string num;
	while(cin >> num && cout << Palindrome(num) << "\n");
	return 0;
}
Last edited on
Topic archived. No new replies allowed.