hours

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • muna123
    New Member
    • Oct 2006
    • 5

    #1

    hours

    Hi, i need help in c prog not c ++ so please don't tell me c ++ code :D

    m_hrs is 8.

    hrs is double

    hrs= (m_hrs * 20) / 60;

    printf(" hrs is %.2lf", hrs);

    ans: 2.00 not 2.67

    the problem is i am not getting 2.67 but when i calculated with calculator i am getting 2.667.

    Thanks for help
  • arne
    Recognized Expert Contributor
    • Oct 2006
    • 315

    #2
    Originally posted by muna123
    Hi, i need help in c prog not c ++ so please don't tell me c ++ code :D

    m_hrs is 8.

    hrs is double

    hrs= (m_hrs * 20) / 60;

    printf(" hrs is %.2lf", hrs);

    ans: 2.00 not 2.67

    the problem is i am not getting 2.67 but when i calculated with calculator i am getting 2.667.

    Thanks for help
    You do the calculation (m_hrs * 20) / 60 with integers (I guess m_hours has type int?) and cast the result to double. You can either define m_hrs to be of type double or change your code to

    Code:
    hrs = ((double)m_hrs*20/60);

    Comment

    • muna123
      New Member
      • Oct 2006
      • 5

      #3
      Originally posted by arne
      You do the calculation (m_hrs * 20) / 60 with integers (I guess m_hours has type int?) and cast the result to double. You can either define m_hrs to be of type double or change your code to

      Code:
      hrs = ((double)m_hrs*20/60);

      Thanks it worked !!!

      Comment

      • arne
        Recognized Expert Contributor
        • Oct 2006
        • 315

        #4
        Originally posted by muna123
        Thanks it worked !!!
        I hope you have understood why? :)

        Comment

        Working...