Bird
0
0

You want to print a floating-point number pi with 2 decimal places using printf. Which code is correct?

hard📝 Application Q15 of 15
C - Input and Output
You want to print a floating-point number pi with 2 decimal places using printf. Which code is correct?
Aprintf("%.2f", pi);
Bprintf("%2f", pi);
Cprintf("%f.2", pi);
Dprintf("%f", pi, 2);
Step-by-Step Solution
Solution:
  1. Step 1: Understand floating-point formatting

    To print a float with 2 decimals, use %.2f in printf.
  2. Step 2: Check each option's correctness

    printf("%.2f", pi); uses %.2f correctly. printf("%2f", pi); misses the dot, C has wrong syntax, D passes extra argument incorrectly.
  3. Final Answer:

    printf("%.2f", pi); -> Option A
  4. Quick Check:

    Use %.2f for 2 decimal float output [OK]
Quick Trick: Use %.2f to limit float decimals to 2 [OK]
Common Mistakes:
  • Omitting the dot before precision
  • Passing extra arguments to printf
  • Using wrong placeholder format

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More C Quizzes