|
-
September 11th, 2010, 07:39 AM
#1
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.
-
September 11th, 2010, 08:16 AM
#2
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.
-
September 11th, 2010, 08:50 AM
#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
-
September 11th, 2010, 12:24 PM
#4
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();
}
-
September 11th, 2010, 01:29 PM
#5
Re: problem with dividing int numbers in C#
-
September 11th, 2010, 03:27 PM
#6
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.
-
September 11th, 2010, 10:09 PM
#7
Re: problem with dividing int numbers in C#
 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.
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|