Bird
0
0

Which nested conditional structure is correct?

hard📝 Application Q8 of 15
C - onditional Statements

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:
  1. 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.
  2. Step 2: Check inner condition for evenness

    Inside positive block, it checks even or odd correctly.
  3. 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
  4. 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

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More C Quizzes