Welcome to Dream.In.Code
Become a Java Expert!

Join 149,954 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,335 people online right now. Registration is fast and FREE... Join Now!




Print All 5 digit Palindromes

 
Reply to this topicStart new topic

Print All 5 digit Palindromes, Creating a for loop that will display all possible palindromes from a

fletchluv
17 Oct, 2008 - 05:24 PM
Post #1

New D.I.C Head
*

Joined: 3 Oct, 2008
Posts: 15


My Contributions
Hello I am trying make a program that will create all possible palindromes. The code will compile, but it won't display the numbers correctly and it will eventually crash. Am I on the right path here? Here is my code:

import java.util.*;

class palindrome
{
public static void main(String[] args)
{
int n,digit1,digit2,digit3,digit4,digit5;

for (n=10000; n <= 99999;n++)
{
digit5 = n % 10;
n = n / 10;
digit4 = n % 10;
n = n /10;
digit3 = n % 10;
n = n / 10;
digit2 = n % 10;
n = n / 10;
digit1 = n % 10;
n = n / 10;
String total = String.valueOf(digit1)+String.valueOf(digit2) +String.valueOf (digit3)+String.valueOf(digit4)+String.valueOf(digit5);
int sum = Integer.parseInt(total);

if((digit1 == digit5) && (digit2 == digit4));
{
System.out.println(" Palindrome is" + sum);
}
}
}
}

I have attached a screen shot of the code if that would be easier to read than what I have here. Any advice or guidance you can provide would be great. Thank you.





Attached File(s)
Attached File  Java_Class_Palindrome_problem.doc ( 88k ) Number of downloads: 3
User is offlineProfile CardPM
+Quote Post

fsloke
RE: Print All 5 Digit Palindromes
17 Oct, 2008 - 06:09 PM
Post #2

D.I.C Regular
***

Joined: 19 Dec, 2007
Posts: 258



Thanked: 4 times
My Contributions
Here is the problem

Problem:
1. You are confusing the variable n looping, so I add in sample variable to maintain the input value.
2. if((digit1 == digit5) && (digit2 == digit4));
I deleted the semicolon.
3. total is same as sum ==> both use for display so I erase the sum variable.

Here is the solution:

CODE

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

/**
*
* @author Foo Soon
*/
public class palindrome {
    public static void main(String[] args)
    {
        int n,digit1,digit2,digit3,digit4,digit5;

        int sample;
        for (sample=10000; sample <= 99999;sample++)
        {
            n=sample;
            digit5 = n % 10; // 0
            n = n / 10; // 1000
            digit4 = n % 10; // 0
            n = n /10; //100
            digit3 = n % 10; //0
            n = n / 10; //10
            digit2 = n % 10; //0
            n = n / 10; //1
            digit1 = n % 10; //1
            n = n / 10; //0
            String total = String.valueOf(digit1)+String.valueOf(digit2) +String.valueOf (digit3)+String.valueOf(digit4)+String.valueOf(digit5);
    
            if((digit1 == digit5) && (digit2 == digit4))
            {
                System.out.println(" Palindrome is" + total);
            }
        }
    }
}



Next time, Please put your code into <code> tag

Thank you

This post has been edited by fsloke: 17 Oct, 2008 - 06:24 PM
User is offlineProfile CardPM
+Quote Post

fletchluv
RE: Print All 5 Digit Palindromes
17 Oct, 2008 - 06:21 PM
Post #3

New D.I.C Head
*

Joined: 3 Oct, 2008
Posts: 15


My Contributions
Sorry im new to this is this what you mean?

CODE


import java.util.*;

class palindrome
{
public static void main(String[] args)
{
int n,digit1,digit2,digit3,digit4,digit5;

for (n=10000; n <= 99999;n++)
{
digit5 = n % 10;
n = n / 10;
digit4 = n % 10;
n = n /10;
digit3 = n % 10;
n = n / 10;
digit2 = n % 10;
n = n / 10;
digit1 = n % 10;
n = n / 10;


String total = String.valueOf(digit5)+String.valueOf(digit4)+String.valueOf(digit3)+String.valueOf(digit2)+String.valueOf(digit1);
int sum = Integer.parseInt(total);

if((digit1 == digit5) && (digit2 == digit4));
{
System.out.println(" Palindrome is" + digit1 + digit5 + digit2 + digit4);
}
}
}
}


User is offlineProfile CardPM
+Quote Post

fsloke
RE: Print All 5 Digit Palindromes
17 Oct, 2008 - 06:26 PM
Post #4

D.I.C Regular
***

Joined: 19 Dec, 2007
Posts: 258



Thanked: 4 times
My Contributions
Sorry I not realize that I you repost already while I edit my post sleep.gif""

Anyway, I already gave you the solution

Please refer on my previous post.

Thank you

-fsloke
User is offlineProfile CardPM
+Quote Post

fletchluv
RE: Print All 5 Digit Palindromes
17 Oct, 2008 - 06:53 PM
Post #5

New D.I.C Head
*

Joined: 3 Oct, 2008
Posts: 15


My Contributions
I see how you did that thank you for your assistance. I really learned alot. Thanks again.


User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/8/09 06:02PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month