I have a problem when I try to solve simple equation. My equation is find the percentage of student marks. This is my code:
using System;
using System.Collection
You're performing division between two integers - which means the result is computed as an integer too. 300 / 1000 is 0... and then you multiply that by 10, which is still 0.
Options:
Use floating point arithmetic. You could do this by making all the variables double
, or by casting:
ret = (int) ((oneee / (double) two) * 10);
(Other floating point types would work too, such as decimal
or float
... They may give very slightly different results in some cases.)
Multiply by 10 first:
res = (oneee * 10) / two;
Note that for a percentage, you'd want to multiply by 100, not 10. Multiplying first is probably simpler than using floating point if you only want an integer result, and if you know that the multiplication will never overflow.
Also note that to experiment with things like this quickly, it's much simpler to use a console app than Windows Forms. Here's a complete example showing it working:
using System;
class Test
{
static void Main()
{
int oneee = 300;
int two = 1000;
int res = (oneee * 10) / two;
Console.WriteLine(res); // 3
}
}
EDIT: If you were intending to use the p format specifier, you should be using a floating point number instead:
int marksScored = 300;
int marksAvailable = 1000;
double proportion = ((double) marksScored) / marksAvailable;
string text = proportion.ToString("p1"); // "30.0%"
Note that only one operand of the division operator needs to be a double
- although you could cast both of them if you find that clearer.