LearnFrenzy
Sign in Take a free test

Floating Point Issues · Find the Error · Question 3

Bug: area = 1/2 * base * height Answer, explanation & discussion

  • Find the error
  • Easy
  • 0 comments
  • Answer explained
  • Moderated community
Question 3 Back to Exercise

What does this program print for a triangle with base 10 and height 6, and why?

main.c
#include <stdio.h>

int main(void) {
    double base = 10, height = 6;
    double area = 1 / 2 * base * height;
    printf("%.1f\n", area);
    return 0;
}
  • A 30.0
  • B 15.0
  • C 0.0 – 1 / 2 is integer division (0), evaluated before the doubles join in Correct
  • D It does not compile
Correct Answer: Option C

Operators of equal precedence group left to right, so 1 / 2 is computed first, in int: 0. Write 0.5 * base * height or base * height / 2.

Discussion & Comments 0

Share your method, a shortcut, or ask about a step you didn't follow.

No comments yet. Be the first to share your solution!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links