How to get decimal division with 2 variables

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Snip
    New Member
    • Oct 2014
    • 8

    How to get decimal division with 2 variables

    How do i get this with decimal part?:

    Code:
    for(i=1;i<=4;i++){
          printf("%d\n",m);
          s = s + m/i
          m = m + 2;
          
          }
    m/i??
  • donbock
    Recognized Expert Top Contributor
    • Mar 2008
    • 2427

    #2
    What type are variables i, m, and s?
    Do you want to display the result as a decimal number (8/5 = 1.6) or as a quotient and remainder (8/5 = 1 R 3)?

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      This is a recurring topic.

      Integer division does not have decimals because integers have no decimals. 3/2 is 1. There is one 2 in 3. In this context 1.5 has no meaning.

      Floating point has decimals and here it would be 3.0/2.0 is 1.5.

      Comment

      • Snip
        New Member
        • Oct 2014
        • 8

        #4
        I was trying to get the result as a decimal number, I did like this:

        m/(float)(i)

        Comment

        • weaknessforcats
          Recognized Expert Expert
          • Mar 2007
          • 9214

          #5
          How are you displaying the result of this?

          Comment

          Working...