C - LoopsYou want to print all odd numbers from 1 to 9 using a for loop. Which code snippet correctly does this?AAll of the aboveBfor(int i = 0; i <= 9; i++) { if(i % 2 == 1) printf("%d ", i); }Cfor(int i = 1; i <= 9; i++) { if(i % 2 != 0) printf("%d ", i); }Dfor(int i = 1; i < 10; i += 2) { printf("%d ", i); }Check Answer
Step-by-Step SolutionSolution:Step 1: Analyze each code snippetfor(int i = 1; i < 10; i += 2) { printf("%d ", i); } increments by 2 starting at 1, printing odd numbers. for(int i = 0; i <= 9; i++) { if(i % 2 == 1) printf("%d ", i); } checks oddness inside loop. for(int i = 1; i <= 9; i++) { if(i % 2 != 0) printf("%d ", i); } also checks oddness with != 0.Step 2: Confirm all snippets print odd numbers 1 to 9All three approaches correctly print odd numbers from 1 to 9.Final Answer:All of the above -> Option AQuick Check:Multiple ways to print odd numbers [OK]Quick Trick: Use increment by 2 or condition check for odds [OK]Common Mistakes:Using wrong increment stepIncorrect condition for odd numbersOff-by-one errors in loop bounds
Master "Loops" in C9 interactive learning modes - each teaches the same concept differentlyLearnWhyDeepVisualTryChallengeProjectRecallTime
More C Quizzes C Basics and Execution Environment - Structure of a C program - Quiz 10hard Conditional Statements - If–else statement - Quiz 2easy Conditional Statements - If statement - Quiz 12easy Conditional Statements - Nested conditional statements - Quiz 12easy Conditional Statements - Nested conditional statements - Quiz 6medium Conditional Statements - If–else statement - Quiz 11easy Loops - While loop - Quiz 6medium Loops - Do–while loop - Quiz 13medium Variables and Data Types - Type modifiers - Quiz 3easy Variables and Data Types - Variable declaration and initialization - Quiz 5medium