The Artima Developer Community
Sponsored Link

Java Answers Forum
Random Numbers and arrays

4 replies on 1 page. Most recent reply: Jan 4, 2010 10:26 PM by Aaron S

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 4 replies on 1 page
Aaron S

Posts: 6
Nickname: bigtex1973
Registered: Dec, 2009

Random Numbers and arrays Posted: Dec 31, 2009 3:27 PM
Reply to this message Reply
Advertisement
I have an assignment write a program that generates an array of size 25 that is filled with randomly generated 5-digit palindrones. I then have to print how many are even and how many are odd. Is there anyone who can help?


Aaron S

Posts: 6
Nickname: bigtex1973
Registered: Dec, 2009

Re: Random Numbers and arrays Posted: Jan 1, 2010 1:00 PM
Reply to this message Reply
I finally got something started. This is what I have so far, but I can't figure out how to load only the palindrones into the array.

import java.util.*;
class working
{
public static void main(String args[])
{
int a[]=new int[25];
for(int i=0;i<25;i++)a=(int)Math.floor(Math.random()*100000+1);

int value1;
int value2;
int digit1;
int digit2;
int digit3;
int digit4;
int digit5;
int n;

for(int i=0;i<25;i++)
{
n=a;
value1=n;
value2=n;
digit1=value1%10;
value1=value1/10;
digit2=value1%10;
value1=value1/10;
digit3=value1%10;
value1=value1/10;
digit4=value1%10;
value1=value1/10;
digit5=value1%10;
value1=value1/10;

if((digit1==digit5)&&(digit2==digit4))
a=n;
}

for(int i=0;i<25;i++)
if (a%2==0)
System.out.printf("\n%10d is even.",a);
else
System.out.printf("\n%10d is odd.",a);
}


}

Vincent O'Sullivan

Posts: 724
Nickname: vincent
Registered: Nov, 2002

Re: Random Numbers and arrays Posted: Jan 2, 2010 1:31 PM
Reply to this message Reply
Instead of trying to generate a five digit number and testing to see if it is a palindrome... Generate a three digit number and then add a fourth digt which is the same as the second one and a fifth digit which is the same as the first.

By doing this you, all you numbers will be palindromes and you won't need to test them.

George B

Posts: 24
Nickname: hmgeorge
Registered: Feb, 2008

Re: Random Numbers and arrays Posted: Jan 2, 2010 1:46 PM
Reply to this message Reply
import java.util.*;
 
class Working
{
    public static void main(String args[])
    {
        int a[] = new int[25];
        int evenCount = 0;
 
        for(int i = 0; i < 25; i++) {
            int digit1 = (int)(Math.random() * 10);
            int digit2 = (int)(Math.random() * 10);
            int digit3 = (int)(Math.random() * 10);
            int digit4 = digit2;
            int digit5 = digit1;
 
            a[i] = digit1 * 10000 +
                   digit2 *  1000 +
                   digit3 *   100 +
                   digit4 *    10 +
                   digit5;
 
            System.out.printf("%05d\n", a[i]);
 
            if ((digit5 % 2) == 0)
                evenCount++;
        }
        System.out.printf("%10d are even.\n", evenCount);
        System.out.printf("%10d are odd.\n", 25 - evenCount);
    }
}

Aaron S

Posts: 6
Nickname: bigtex1973
Registered: Dec, 2009

Re: Random Numbers and arrays Posted: Jan 4, 2010 10:26 PM
Reply to this message Reply
Vincent and George,

Thanks so much for your help. This is a lot easier than I was trying.

Flat View: This topic has 4 replies on 1 page
Topic: Temperature Tracking Program Previous Topic   Next Topic Topic: How to turn Photos into  photo slideshow?

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use