📖 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
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
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
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
Hint
Use printf with %d to show the number stored in count_even.
Practice
(1/5)
1. What does a for loop in C primarily do?
easy
A. Repeats a block of code a fixed number of times
B. Defines a new variable
C. Stops the program execution
D. Declares a function
Solution
Step 1: Understand the purpose of a for loop
A for loop is designed to repeat a block of code multiple times based on a condition.
Step 2: Identify the correct description
Among the options, only repeating code a fixed number of times matches the for loop's purpose.
Final Answer:
Repeats a block of code a fixed number of times -> Option A
Quick Check:
For loop = repeat code [OK]
Hint: For loops repeat code a set number of times [OK]
Common Mistakes:
Confusing for loop with variable declaration
Thinking for loop stops program
Mixing for loop with function declaration
2. Which of the following is the correct syntax for a for loop in C?
easy
A. for i = 0 to 5 { }
B. for (int i = 0; i++ < 5;) { }
C. for (i < 5; i = 0; i++) { }
D. for (int i = 0; i < 5; i++) { }
Solution
Step 1: Review standard for loop syntax in C
The correct syntax is: for (initialization; condition; update) { body }
Step 2: Check each option
for (int i = 0; i < 5; i++) { } matches the correct syntax with initialization, condition, and update in proper order.
Final Answer:
for (int i = 0; i < 5; i++) { } -> Option D
Quick Check:
Syntax = for(init; cond; update) [OK]
Hint: For loop syntax: for(init; condition; update) [OK]
Common Mistakes:
Using 'for i = 0 to 5' which is not C syntax
Swapping condition and initialization
Missing semicolons inside for parentheses
3. What is the output of this code?
for(int i = 1; i <= 3; i++) {
printf("%d ", i);
}
medium
A. 3 2 1
B. 0 1 2 3
C. 1 2 3
D. 1 2 3 4
Solution
Step 1: Analyze the loop range
The loop starts at i=1 and runs while i is less than or equal to 3, incrementing i by 1 each time.
Step 2: Determine printed values
It prints i each time: 1, then 2, then 3, then stops.
Final Answer:
1 2 3 -> Option C
Quick Check:
Loop prints 1 to 3 [OK]
Hint: Check loop start, end, and increments carefully [OK]
Common Mistakes:
Starting loop at 0 instead of 1
Including an extra number beyond 3
Printing in reverse order
4. Identify the error in this for loop:
for(int i = 0; i < 5; i)
printf("%d ", i);
medium
A. Missing semicolon after printf statement
B. Missing increment operator in for loop update
C. Incorrect loop condition syntax
D. Variable i not declared
Solution
Step 1: Examine the for loop update part
The update section is 'i' without increment operator, so i never changes.
Step 2: Understand the effect of missing increment
Without 'i++' or similar, the loop runs infinitely or behaves incorrectly.
Final Answer:
Missing increment operator in for loop update -> Option B
Quick Check:
Update must change loop variable [OK]
Hint: For loop update must change variable (e.g., i++) [OK]
Common Mistakes:
Forgetting to increment loop variable
Confusing semicolon placement
Assuming variable auto-increments
5. How can you use a for loop to print only even numbers from 2 to 10 inclusive in C?
hard
A. for(int i = 2; i <= 10; i += 2) { printf("%d ", i); }
B. for(int i = 0; i <= 10; i++) { if(i % 2 == 0) printf("%d ", i); }
C. for(int i = 2; i < 10; i++) { if(i % 2 == 0) printf("%d ", i); }
D. for(int i = 0; i <= 10; i += 3) { printf("%d ", i); }
Solution
Step 1: Understand the goal
We want to print even numbers from 2 to 10 inclusive.
Step 2: Check each option
for(int i = 2; i <= 10; i += 2) { printf("%d ", i); } starts at 2 and increments by 2, printing 2,4,6,8,10 exactly.
Final Answer:
for(int i = 2; i <= 10; i += 2) { printf("%d ", i); } -> Option A
Quick Check:
Increment by 2 prints evens directly [OK]
Hint: Increment by 2 to print evens directly [OK]
Common Mistakes:
Using wrong increment step
Off-by-one errors in loop condition
Printing odd numbers by mistake
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.