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

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




5 Digit palindrome array problem

 
Reply to this topicStart new topic

5 Digit palindrome array problem

JayCom1969
16 Jun, 2008 - 11:26 AM
Post #1

New D.I.C Head
*

Joined: 16 Jun, 2008
Posts: 3


My Contributions
After looking through the forum I wasn't able to find anyone that has had the issue that I'm having.

I need to write a program to initialize an array of size 25, and then place a palindrome in each spot. After that I need to determine whether they are even or odd, and give the totals of each.

My thought process was to get a random 5 digit number, then determine the digits in it. After that I had the program look at digit 1 and digit 5, and if they weren't equal to each other then have it go back to the part where it generated a random number again.

The program compiles fine, but when I run it all I get for an output is a couple of palindromes and a bunch of zeros.

I think my problem is in the area where I do the digits not being equal to each other... I though that if I had an if statement to do the comparison (not equal to each other) followed by a continue that it would go back to the top of the loop. It just doesn't seem to be doing that.

If the number generated isn't a palindrome it looks to me that it just continues, but it does work when the randomly generated number is a palindrome the first time.

Thanks for any help, my code follows.

Jay

CODE

class Mod7Prob1

{

    public static void main(String args[])

    {

        // Declaring variables

        int digit1, digit2, digit3, digit4, digit5;

        int even, odd;

        int number, i, n;

        int pals[];

        pals = new int[25];

        i = 0;

        even = 0;

        odd = 0;

                        

        // While loop to get palindromes

        while (i<25)

        {

            // Getting random 5 digit number

            n = 0;

            n = (int)Math.floor(Math.random()*100000+1);

                        

            // Determining digits in number

            number = n;

            digit1 = n%10;

            n = n/10;

            digit2 = n%10;

            n = n/10;

            digit3 = n%10;

            n = n/10;

            digit4 = n%10;

            digit5 = n/10;

            

            // Using if logic to determine palindrome

            if (digit1 != digit5) continue;

            if (digit1 == digit5)

            {

                if (digit2 == digit4)

                    {

                        pals [i] = number;

                        if (number%2 == 0)

                        {

                            even++;

                        }

                        else

                        {

                            odd++;

                        }

                    }

            }

            

            i++;

        }

        System.out.println("Palindromes are: ");

        for (i=0; i<25; i++)

        {

            System.out.print(pals[i]+" ");

        }

        System.out.println("");

        System.out.println("Number of even palindromes "+even);

        System.out.println("Number of odd palindromes "+odd);

    }

}


This post has been edited by JayCom1969: 16 Jun, 2008 - 01:01 PM
User is offlineProfile CardPM
+Quote Post

nick2price
RE: 5 Digit Palindrome Array Problem
16 Jun, 2008 - 11:45 AM
Post #2

D.I.C Regular
***

Joined: 23 Nov, 2007
Posts: 338



Thanked: 12 times
My Contributions
Heres a good example you could just modify to suit your needs. What it basically does is reverse the number then checks it against the original number.
CODE
public class Palindrome  {
      public static void main(String [] args){
      try{
      BufferedReader object = new BufferedReader(
                  new InputStreamReader(System.in));
      System.out.println("Enter number");
      int num= Integer.parseInt(object.readLine());
        int n = num;
        int rev=0;
        System.out.println("Number: ");
        System.out.println(" "+ num);
        for (int i=0; i<=num; i++){
          int r=num%10;
          num=num/10;
          rev=rev*10+r;
          i=0;
        }
        System.out.println("After reversing the number: "+ " ");
        System.out.println(" "+ rev);      
        if(n == rev){
        System.out.print("Number is palindrome!");
        }
        else{
        System.out.println("Number is not palindrome!");
        }
      }
      catch(Exception e){
        System.out.println("Out of range!");
      }
  }
}  


This post has been edited by nick2price: 16 Jun, 2008 - 11:47 AM
User is offlineProfile CardPM
+Quote Post

JayCom1969
RE: 5 Digit Palindrome Array Problem
16 Jun, 2008 - 01:26 PM
Post #3

New D.I.C Head
*

Joined: 16 Jun, 2008
Posts: 3


My Contributions
I figured it out... I had the program go back to the top of the while loop if digit1 did not equal digit5, but I didn't address what to do if digit4 didn't equal digit2.

I threw in an "else continue" statement in line with digit2 being equal to digit4 and it runs like a charm... I just needed to walk away and have a beer for it to make sense!

So here's what I ended up with. Pretty clunky, but it works...

CODE

class Mod7Prob1

{

    public static void main(String args[])

    {

        // Declaring variables

        int digit1, digit2, digit3, digit4, digit5;

        int even, odd;

        int number, i, n;

        int pals[];

        pals = new int[25];

        i = 0;

        even = 0;

        odd = 0;

                        

        // While loop to get palindromes

        while (i<25)

        {

            // Getting random 5 digit number

            n = 0;

            n = (int)Math.floor(10000+Math.random()*(99999-10000+1));

                        

            // Determining digits in number

            number = n;

            digit1 = n%10;

            n = n/10;

            digit2 = n%10;

            n = n/10;

            digit3 = n%10;

            n = n/10;

            digit4 = n%10;

            digit5 = n/10;

            

            // Using if logic to determine palindrome

            if (digit1 != digit5) continue;

            if (digit1 == digit5)

            {

                if (digit2 == digit4)

                    {

                        pals [i] = number;

                        if (number%2 == 0)

                        {

                            even++;

                        }

                        else

                        {

                            odd++;

                        }

                    }

                else continue;

            }

            

            i++;

        }

        System.out.println("Palindromes are: ");

        for (i=0; i<25; i++)

        {

            System.out.println(pals[i]+" ");

        }

        System.out.println("");

        System.out.println("Number of even palindromes "+even);

        System.out.println("Number of odd palindromes "+odd);

    }

}


User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 12:30AM

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