Answer to Question #3481 in Java | JSP | JSF for Y mukundareddy

Question #3481
(4) Write a program to store n elements in an array and retrieve the even numbers from that array.
1
Expert's answer
2011-08-07T07:20:16-0400
import java.util.Random;

public class Task11
{
public static void main(String[] args)
{
& Random generator = new Random(100);

& // The length of the array
& int n = generator.nextInt(20) + 10;

& // Generating the elements of the array
& int[] numbers = new int[n];
& System.out.println("The elements of the generated array: ");
& for (int i = 0; i < n; i++)
& {
& numbers[i] = generator.nextInt(1000);
& System.out.print(numbers[i]);
& if (i < n - 1)
& System.out.print(", ");
& }

& // Outputing the even elements of the array
& System.out.println("\n\nThe even elements of the array: ");
& for (int number : numbers)
& if (number % 2 == 0)
& System.out.print(number + ", ");
& System.out.println("\b\b");
&
}
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS