Buzz Code
Published in

Buzz Code

Java 8 | Array Practice

Hi guys! Welcome to another java exercise with me! Today let’s practice some more on array. If you missed the basic array sesh, check out my old post below!

[Question 1] Count how many even numbers in the array below and print it out.

int[] a = {1, 2, 3, 4, 5, 6, 7};

I know it’s way easier to count the numbers with your brain, but imagine when there are more than thousand random numbers and you have to count the even numbers in those by your hands and eyes…Let’s build this program for that. The answer is on below, and you know the drill!

I put the variable cnt to count the number of the even numbers, and I put the variable i as the index of the array, So the for statement will check all the indexes in order. And I used the if statement to make cnt bigger if the value is a even number.

[Question 2] Which index has 4 as a value?

int[] a = {1, 2, 3, 4, 5, 6, 7};

This time we have to make a program that prints the index number. The answer is on below.

Because the index starts with 0, so a[0] has 1, a[1] has 2….a[3] has 4.

[Question 3] Use the array below, get the result of the following equation.

int[] a = {3, 4, 2};


Oooooh this one is tricky but interesting! We have to multiply the numbers from 1 to the value each array has and sum all the numbers from that!

I declared the variable sum to get the sum of all the numbers and i is for counting indexes as usual. And I declared another variable mul which is for getting the multiplied number inside of the first for statement, because when the index changed the mul has to be back in 1 again. And I put the j to count numbers from 1 to each value.

That’s all for today guys! If you solved all the questions today, great job! We will finally generate the lottery program next time! It’s gonna be super fun! Thanks guys, see ya!



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store