0
0
Cprogramming~20 mins

For loop in C - Mini Project: Build & Apply

Choose your learning style9 modes available
Counting Even Numbers with a For Loop in C
📖 Scenario: You have a list of numbers representing daily sales counts in a small shop. You want to find out how many days had an even number of sales.
🎯 Goal: Build a C program that counts how many numbers in a given list are even using a for loop.
📋 What You'll Learn
Create an integer array called sales with the exact values: 12, 7, 9, 20, 15
Create an integer variable called count_even and set it to 0
Use a for loop with an integer variable i to iterate over the sales array
Inside the loop, use an if statement to check if the current number is even
Increase count_even by 1 for each even number found
Print the final value of count_even using printf
💡 Why This Matters
🌍 Real World
Counting specific types of data in a list is common in many real-world tasks, like analyzing sales, temperatures, or survey results.
💼 Career
Understanding loops and conditions is essential for programming jobs, as they help automate repetitive tasks and data analysis.
Progress0 / 4 steps
1
Create the sales data array
Create an integer array called sales with these exact values: 12, 7, 9, 20, 15.
C
Need a hint?

Use curly braces {} to list the numbers inside the array.

2
Create a counter variable
Create an integer variable called count_even and set it to 0.
C
Need a hint?

This variable will keep track of how many even numbers you find.

3
Use a for loop to count even numbers
Use a for loop with an integer variable i to iterate over the sales array. Inside the loop, use an if statement to check if sales[i] is even (divisible by 2). If it is even, increase count_even by 1.
C
Need a hint?

Remember, use i < 5 because the array has 5 elements indexed 0 to 4.

4
Print the count of even numbers
Write a printf statement to print the value of count_even with the exact text: "Number of even sales days: %d\n".
C
Need a hint?

Use printf with %d to show the number stored in count_even.