CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Sep 2010
    Posts
    3

    problem with dividing int numbers in C#

    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. #2
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: problem with dividing int numbers in C#

    It would appear that you are writing to the screen before you assign a value to the variable. Result is that you are writing 0 to the screen and then doing the calculation but not displaying the result.
    Always use [code][/code] tags when posting code.

  3. #3
    Join Date
    Sep 2010
    Posts
    3

    Re: problem with dividing int numbers in C#

    Code:
    using System;
    class eng
    {
     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());
       division = (divident / divisor);
       Console.Write("division: " +division);
       rest = (divident % divisor);
       Console.Write(" rest: " +rest);
      }
    }
    you mean like this? It still returns 0

  4. #4
    Join Date
    Sep 2010
    Posts
    7

    Re: problem with dividing int numbers in C#

    i corrected your code

    static void Main()
    {
    int divident = 0;
    int divisor = 0;
    double division = 0;
    int rest = 0;
    Console.Write("divident: ");
    divident = Convert.ToInt32(Console.ReadLine());
    Console.Write("divisor: ");
    divisor = Convert.ToInt32(Console.ReadLine());
    division = ((double)divident / (double)divisor);
    Console.Write("division: " + division);
    rest = (divident % divisor);
    Console.Write(" rest: " + rest);
    Console.Read();
    }

  5. #5
    Join Date
    Sep 2010
    Posts
    3

    Re: problem with dividing int numbers in C#

    thank you sir

  6. #6
    Join Date
    Jun 2008
    Posts
    2,477

    Re: problem with dividing int numbers in C#

    Do you know why that was happening in the first place though? That is more important than getting handed the answer.

  7. #7
    Join Date
    Jan 2010
    Posts
    1,133

    Re: problem with dividing int numbers in C#

    Quote Originally Posted by BigEd781 View Post
    Do you know why that was happening in the first place though? That is more important than getting handed the answer.
    Agreed, and I can't stress that enough! (and I'm not referring to the inherent limitations of the text formatting tools )
    If not, ofeigur you'll be scratching your head for the same reason on the very next assignment (or the one after that, if you get "lucky").
    And once you've got it, you'll find this knowledge rather useful.
    Last edited by TheGreatCthulhu; September 11th, 2010 at 10:12 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured