15,905,563 members
See more:
C#
```class Program {
public static void Main (string[] args) {
uint number = (uint)34;
uint result = number;
for (uint i = 1; i < number; i++)
{
result = result * (number - i);
//Console.WriteLine(result);

}
Console.WriteLine(result);
}
}```

The execution of this algorithm, need return more than 0 because I used Uint. I started now to study C#, and I dont now how can I do to solve this problem.

What I have tried:

I think the problem is on convert
Posted
Updated 20-Nov-22 3:10am
v2

Solution 1

Change your code a little bit so you can see what the values are. The problem should become obvious:
C#
```Console.WriteLine(\$"The maximum value for a uint is: {uint.MaxValue}");

uint number = (uint)32;
uint result = number;
for (uint i = 1; i < number; i++)
{
result = result * (number - i);
Console.WriteLine(\$"result: {result}\tnumber: {number}\ti: {i}");

}
Console.WriteLine(result);```

If you exceed the maximum value for a type, the value "rolls over" and starts again and the lowest value for that type.

v2
George Swan 20-Nov-22 4:26am
You need to use the type ulong to prevent an overflow
Dave Kreskowiak 20-Nov-22 10:58am
Yeah, but that wasn't the point of the answer. It was for the OP to think about the problem and why he's seeing the result he is.

Oh, and even a ulong cannot hold the resulting answers. This one would have to go to the BigInt to solve.
George Swan 20-Nov-22 11:32am
You are quite correct Dave. With a ulong it overflows on the 13th iteration. I assumed when I should have tested. My apologies. Regards George.

Solution 3

For big numbers try like this:
```using System;
using System.Diagnostics;
using System.Numerics;

namespace ConsoleApp1
{
internal class Program
{
static void Main(string[] args)
{
uint number = 34;
BigInteger result = number;

for (uint i = 1; i < number; i++)
{
result = result * (number - i);
Debug.Print(\$"{i} {result}");
}

Console.WriteLine(result);
}
}
}```

v2