LearnFrenzy
Sign in Take a free test

Operators and Expressions · Find the Error · Question 3

Error: using % with a double operand Answer, explanation & discussion

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

The program below has a bug. Which statement correctly describes it?

main.c
#include <stdio.h>

int main(void) {
    double minutes = 135.5;
    double rest = minutes % 60;
    printf("%.1f\n", rest);
    return 0;
}
  • A 60 must be 60.0
  • B The % operator requires integer operands; use fmod(minutes, 60) from <math.h> Correct
  • C rest must be an int
  • D There is no bug
Correct Answer: Option B

Even writing 60.0 does not help: % is not defined for floating types. fmod(135.5, 60) returns 15.5.

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