0% found this document useful (0 votes)
2 views4 pages

Count Even Numbers in an Array

Uploaded by

marino.amandam
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views4 pages

Count Even Numbers in an Array

Uploaded by

marino.amandam
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

‭WEEK #18 – Counting Even Numbers‬

T‭ his worksheet focuses on looping through an array, checking each value, and counting how‬
‭many satisfy a certain condition. This is a classic programming pattern used everywhere — from‬
‭inventory management in games to data filtering and AI decision logic.‬
‭Let’s fully break down the code.‬

‭Step 1: Understanding the initial array‬


‭The array named‬‭numberArray‬‭contains:‬
[4, 3, 1, 8, 3, 7, 16]‬

T‭ here are 7 numbers total.‬


‭We set another variable:‬
count = 0‬

T‭ he goal of the program is to loop through the array and increase‬‭count‬‭every time a number is‬
‭even‬‭.‬

‭Step 2: The loop‬


‭The code uses:‬
for element value of numberArray‬

T‭ his means:‬
‭For each value in the array, temporarily store it in the variable‬‭value‬‭, and run the block inside.‬
‭So value takes on each of these numbers in order:‬
‭1.‬ ‭4‬
‭2.‬ ‭3‬
‭3.‬ ‭1‬
‭4.‬ ‭8‬
‭5.‬ ‭3‬
‭6.‬ ‭7‬
‭7.‬ ‭16‬

‭Step 3: The condition‬


‭Inside the loop, we have:‬
if remainder of value / 2 = 0 then‬

change count by 1‬

T‭ his checks whether a number is even.‬
‭Even numbers have‬‭no remainder when divided by 2‬‭.‬
‭Let’s check each number:‬
‭1. 4 → even‬
4‭ / 2 = 2, remainder 0‬
‭count = 1‬
‭2. 3 → odd‬
3‭ / 2 = remainder 1‬
‭count stays = 1‬
‭3. 1 → odd‬
1‭ / 2 = remainder 1‬
‭count stays = 1‬
‭4. 8 → even‬
8‭ / 2 = 4, remainder 0‬
‭count = 2‬
‭5. 3 → odd‬
‭Odd, count stays = 2‬
‭6. 7 → odd‬
‭Odd, count stays = 2‬
‭7. 16 → even‬
1‭ 6 / 2 = 8, remainder 0‬
‭count = 3‬

🎉
‭ Final Answer for Week #18‬
‭The final value of‬‭count‬‭is:‬
3‬

‭Because the even numbers are:‬‭4, 8, 16‬

You might also like