You want to write a program that prints "Positive and even" if a number is greater than zero and even, "Positive and odd" if greater than zero and odd, and "Non-positive" otherwise. Which nested conditional structure is correct?
Aif (num > 0) { if (num % 2 == 0) { printf("Positive and even\n"); } else { printf("Positive and odd\n"); } } else { printf("Non-positive\n"); }
Bif (num % 2 == 0) { if (num > 0) { printf("Positive and even\n"); } else { printf("Non-positive\n"); } } else { printf("Positive and odd\n"); }
Cif (num > 0 && num % 2 == 0) { printf("Positive and even\n"); } else if (num > 0 && num % 2 != 0) { printf("Positive and odd\n"); } else { printf("Non-positive\n"); }
Dif (num > 0) { if (num % 2 != 0) { printf("Positive and even\n"); } else { printf("Positive and odd\n"); } } else { printf("Non-positive\n"); }
Step-by-Step Solution
Solution:
Step 1: Check outer condition for positivity
if (num > 0) { if (num % 2 == 0) { printf("Positive and even\n"); } else { printf("Positive and odd\n"); } } else { printf("Non-positive\n"); } correctly checks if number is positive first.
Step 2: Check inner condition for evenness
Inside positive block, it checks even or odd correctly.
Final Answer:
if (num > 0) { if (num % 2 == 0) { printf("Positive and even\n"); } else { printf("Positive and odd\n"); } } else { printf("Non-positive\n"); } -> Option A
Quick Check:
Outer positive check then inner even/odd [OK]
Quick Trick:Check main condition first, then nested details [OK]
Common Mistakes:
Checking evenness before positivity
Mixing else blocks
Using && instead of nested if
Master "onditional Statements" in C
9 interactive learning modes - each teaches the same concept differently