LearnFrenzy
Sign in Take a free test

Decision Making · Find the Error · Question 5

Bug: integer division inside a percentage condition Answer, explanation & discussion

  • Find the error
  • Medium
  • 0 comments
  • Answer explained
  • Moderated community
Question 5 Back to Exercise

45 of 60 students passed (75%). What does the program print, and why?

main.c
#include <stdio.h>

int main(void) {
    int passed = 45, total = 60;
    if (passed / total * 100 >= 50)
        puts("more than half passed");
    else
        puts("less than half passed");
    return 0;
}
  • A more than half passed
  • B less than half passed – 45 / 60 is 0 in integer division, so the percentage is 0 Correct
  • C It crashes
  • D It does not compile
Correct Answer: Option B

Evaluation goes left to right: 45 / 60 truncates to 0 before the multiplication. Multiply first – passed * 100 / total – or compare without dividing: passed * 2 >= total.

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