# Creating a 5 digit palindrome

Page 1 of 1

## 3 Replies - 4522 Views - Last Post: 11 October 2008 - 08:17 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=67125&amp;s=b68a42c88eaa93fc81763d08e26feebb&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 fletchluv

Reputation: 2
• Posts: 15
• Joined: 03-October 08

# Creating a 5 digit palindrome

Posted 10 October 2008 - 10:24 PM

In my Java class I must create a program that takes a 5 digit number from keyboard input and tells you if the number that was inputted is a palindrome or not. I am very close to making it work, but I am having trouble deriving the 5th digit. I am thinking that is why the program will not calculate if the value is a palindrome or not because it cannot account for the 5th digit. When I use a 4 digit number it works fine. The code does compile correctly. I just need a little guidance to ensure I am on the right path. Here is my code.
```											  import java.util.*;
class palindrome
{
public static void main(String[] args)
{
int n,n1,n2,n3,digit1,digit2,digit3,digit4,digit5;
Scanner keyboard = new Scanner(System.in);
System.out.println("Input a 5 digit number:");
n = keyboard.nextInt();
digit1 = n/1000;
n1 = n % 1000;
digit2 = n1 / 100;
n2 = n1 % 100;
digit3 = n2 / 10;
digit4 = n2 % 10;
n3 = n2 / 1;
digit5 = n3 % 1;
String total = String.valueOf(digit1)+String.valueOf(digit2)
+String.valueOf(digit3)+String.valueOf(digit4)+String.valueOf(digit5);
int sum = Integer.parseInt(total);
System.out.println("The number is " + sum);
if((digit1 == digit5) || (digit5 == digit1))
{
System.out.println("Number is a Palindrome");
}
else
{
System.out.println("Number is not a Palindrome");
}
}
}

```

Any guidance I can get would be great. Thank you for your time.

Thanks - BetaWar

Is This A Good Question/Topic? 0

## Replies To: Creating a 5 digit palindrome

### #2 markhazlett9

• Coding is a lifestyle

Reputation: 61
• Posts: 1,666
• Joined: 12-July 08

## Re: Creating a 5 digit palindrome

Posted 10 October 2008 - 10:28 PM

Is a Palindrone not a word or string just backwards? How will you be able to tell if a number is backwards? Just curious

### #3 fletchluv

Reputation: 2
• Posts: 15
• Joined: 03-October 08

## Re: Creating a 5 digit palindrome

Posted 10 October 2008 - 10:45 PM

Forgive me since I am brand new to Java, from what my instructor had mentioned this is what he said a Palindrone should be:

" Write a program that inputs a 5-digit integer from the keyboard and prints if the
input number is a palindrome or not.
A palindrome is a number, which has the same digit from either end.
Examples:
12921 is a palindrome.
12923 is not a palindrome "

I just created the program to be able to perform that function as he mentioned. Hope this clears it up.

### #4 Gloin

• Expert Schmexpert...

Reputation: 235
• Posts: 4,489
• Joined: 04-August 08

## Re: Creating a 5 digit palindrome

Posted 11 October 2008 - 08:17 AM

Then all you need to do is using the String.charAt(int i) method.

Check that

String.charAt(0) = String.charAt(4)
and
String.charAt(1) = String.charAt(3)

voila!, problem solved