I submitted this once. Please read the question instead of copy and pasting irre
ID: 3533374 • Letter: I
Question
I submitted this once. Please read the question instead of copy and pasting irrelevent code from Google. I can use the internet too. The program populates an array with 5 digit palindromes and returns the values and the number which are odd and even. I've been playing with this for a few hours, but I am having an issue with the do...while loop I believe.
import java.util.*;
public class PalinArray
{
public static void main(String args[])
{
Random r = new Random();
int even = 0;
int odd = 0;
int[] p = new int[25];
for (int i=0; i<25; i++)
p[i] = palin(r);
System.out.println("The numbers are:");
for(int i = 0; i<25; i++)
{
if (p[i]%2 == 0)
even++;
else
odd++;
System.out.print(p[i]+" ");
if ((i+1) == 0)
System.out.println();
}
System.out.println(" " + even + "are even " + odd + "are odd");
}
public static int palin(Random r)
{
int n;
do
{
n=r.nextInt(90000) + 10001;
}
while
(!ispalin(n));
return n;
}
public static boolean ispalin(int n)
{
if(n!=n/10000)
return false;
if(n/10 == n/1000)
return true;
return false;
}
}
Explanation / Answer
i think in the ispalin function you need to omit the last statement return false as then it will retrn false in any situation