problem with dividing int numbers in C#

Discussion in 'C#' started by ofeigur, Sep 11, 2010.

  1. ofeigur

    ofeigur New Member

    Joined:
    Sep 11, 2010
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Code:
    using System;
    class test
    {
     static void Main()
      {
       int divident = 0;
       int divisor = 0;
       int division = 0;
       int rest = 0;
       Console.Write("divident: ");
       divident = Convert.ToInt32(Console.ReadLine());
       Console.Write("divisor: ");
       divisor = Convert.ToInt32(Console.ReadLine());
       Console.Write("division: " +division);
       division = (divident / divisor);
       Console.Write(" rest: " +rest);
       rest = (divident % divisor);
      }
    }
    
    my program always returns 0 in division and the rest and I have no idea what's wrong.
     
  2. Love.NET

    Love.NET New Member

    Joined:
    Nov 20, 2010
    Messages:
    14
    Likes Received:
    0
    Trophy Points:
    0
    Home Page:
    http://sites.google.com/site/esubstance
    The return value of the calculus "a/b" (a and b are integers) is always an integer. If a<b, the return value is always a 0. For Ex: 1/2 = 0 not 0.5 ; 5/2 = 2 not 2.5
    I don't think the rest is the same to the division (returns 0).
    You can use type cast to convert one of two operands (divident or divisor) into another type of number such as float type like as "division = (float) divident/divisor ;"

    the division have to be declared as a float.
    Hope that help !
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice