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.
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.
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
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();
}
Re: problem with dividing int numbers in C#
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.
Re: problem with dividing int numbers in C#
Quote:
Originally Posted by
BigEd781
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.