Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Palindorme no.

 
Pawan Arora
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I've created a programme to figure out is any number is palindrome or not by having a value at command prompt



But when I entered the value 994009, the compiler saying it is a palindrome no. why? Although it is doing fine, when I entered any other non-palindrome no., but why it is not doing the same with the above value?
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This seems odd:


Hint #1: You don't need "last" - it's the same as "e.length-1-start".

Hint #2: You don't need start to run from 0 to e.length-1. It's sufficient for start to run through e.length/2.
[ December 18, 2008: Message edited by: Ulf Dittmer ]
 
Sunil Kumar
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is because


This is because once second character is same as the last one. For any number like 9909, 6644446.. it will consider it as a palindrome.
 
Sunil Kumar
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Further i am surprised to see the way you are saying yes or no.
There could be other ways. Create a method.Simply check for the equality condition, return no if it is unequal , return yes after the for loop completes
[ December 18, 2008: Message edited by: Sunil Kumar ]
 
Pawan Arora
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, Ulf and Sunil. Could you please check, is the following is a correct way to write a palindrome code or not. Although this time it is working correct.


[ December 18, 2008: Message edited by: Pawan Arora ]
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That looks good. Here's a version that's simplified a bit

 
Pawan Arora
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ulf.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic