15,665,942 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 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

## 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.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Top Experts
Last 24hrsThis month
 OriginalGriff 383 CPallini 50 George Swan 50 Andre Oosthuizen 40 Richard Deeming 40
 OriginalGriff 150 CPallini 50 Andre Oosthuizen 40 Richard MacCutchan 20 George Swan 15

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900