1 Replies - 2371 Views - Last Post: 28 October 2007 - 09:58 PM Rate Topic: -----

#1 aaron.henriques  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 20
  • Joined: 20-September 07

palindrom mistake

Post icon  Posted 28 October 2007 - 08:57 PM

hey guys i am really sorry to bring this topic up again but i got by c++ exams coming up soon so i just wanted to clear my doubts. I have checked the previews topics on palindroms but it did not solve my doubts. So please if you guys can help me it will really be good.
The question is to check weather the entered number is a palindrome or not and my code is as belows...

#include<iostream.h>
#include<conio.h>
void main()
{	   
	clrscr();
	int n,num,digit,rev=0;
	cout<<"\n"<<"input the number";
	cin>>num;
	n=num;
	do
	{		
		digit=num%10;
		rev=(rev*10)+digit;
		num=num/10;
	}
	while(num!=0);
		cout<<"the reverse of the number is:"<<rev<<"\n";
	if(n==rev)
		cout<<"the number is palindrome";
	else
		cout<<"the number is not palindrome";
}




In this the mistake is that for some palindromes like "32123" it says yes it is a palindrome. But for others like "4321234" it says no it is not a palindrome. This is the main reason for my confusion. Please some one please help me.

Is This A Good Question/Topic? 0
  • +

Replies To: palindrom mistake

#2 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: palindrom mistake

Posted 28 October 2007 - 09:58 PM

That is an odd one... what I did was edit the definition of the integers you use:

unsigned long int n,num,digit,rev=0;

This seems to fix the error. It was a very odd bug though. I think I will look into it more, perhaps a debugger will help.

The program seems to work unless the number is too large to fit into a long (which my first test did oddly enough).
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1