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