{"record":{"id":"3f1781f38996e10a","repo":"TheAlgorithms/C-Sharp","slug":"only-for-num-0","errorCode":null,"errorMessage":"Only for num >= 0","messagePattern":"Only for num >= 0","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"Algorithms/Numeric/Factorial.cs","lineNumber":22,"sourceCode":"///     The factorial of a positive integer n, denoted by n!,\n///     is the product of all positive integers less than or equal to n.\n/// </summary>\npublic static class Factorial\n{\n    /// <summary>\n    ///     Calculates factorial of a integer number.\n    /// </summary>\n    /// <param name=\"inputNum\">Integer Input number.</param>\n    /// <returns>Factorial of integer input number.</returns>\n    public static BigInteger Calculate(int inputNum)\n    {\n        // Convert integer input to BigInteger\n        BigInteger num = new BigInteger(inputNum);\n\n        // Don't calculate factorial if input is a negative number.\n        if (BigInteger.Compare(num, BigInteger.Zero) < 0)\n        {\n            throw new ArgumentException(\"Only for num >= 0\");\n        }\n\n        // Factorial of numbers greater than 0.\n        BigInteger result = BigInteger.One;\n\n        for (BigInteger i = BigInteger.One; BigInteger.Compare(i, num) <= 0; i = BigInteger.Add(i, BigInteger.One))\n        {\n            result = BigInteger.Multiply(result, i);\n        }\n\n        return result;\n    }\n}\n","sourceCodeStart":4,"sourceCodeEnd":36,"githubUrl":"https://github.com/TheAlgorithms/C-Sharp/blob/96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c/Algorithms/Numeric/Factorial.cs#L4-L36","documentation":"GaussJordanElimination.Solve(matrix) solves a linear system via Gauss-Jordan elimination on an augmented n x (n+1) matrix. Before solving it calls CanMatrixBeUsed, which requires the matrix to be non-empty and shaped n x (n+1); otherwise it throws ArgumentException 'Please use a n*(n+1) matrix with Length > 0.'","triggerScenarios":"Passing a non-augmented n x n coefficient matrix instead of n x (n+1); passing an empty 0-length array (RowCount becomes 0); passing a rectangular matrix whose column count is not rows + 1.","commonSituations":"Forgetting to append the constants column (b) to the coefficient matrix; loading a system from CSV without the RHS column; constructing the array with new double[0,0] when the input was empty or parsing failed.","solutions":["Build the augmented matrix: for a system A*x = b, create an n x (n+1) array where column n holds b.","Check the matrix is non-empty (GetLength(0) > 0 and GetLength(1) == GetLength(0) + 1) before calling Solve.","Inspect CanMatrixBeUsed's shape rules and validate input where it is constructed, e.g. after parsing."],"exampleFix":"// before\nvar solver = new GaussJordanElimination();\nsolver.Solve(a); // a is n x n: throws\n// after\nint n = a.GetLength(0);\nvar augmented = new double[n, n + 1];\nfor (int i = 0; i < n; i++)\n{\n    for (int j = 0; j < n; j++) augmented[i, j] = a[i, j];\n    augmented[i, n] = b[i];\n}\nsolver.Solve(augmented);","handlingStrategy":"validation","validationCode":"bool usable = matrix != null\n    && matrix.GetLength(0) > 0\n    && matrix.GetLength(1) == matrix.GetLength(0) + 1;\nif (!usable)\n    throw new ArgumentException(\"Solve requires a non-empty n x (n+1) augmented matrix\");","typeGuard":"static bool IsAugmentedMatrix(double[,] m) =>\n    m != null && m.GetLength(0) > 0 && m.GetLength(1) == m.GetLength(0) + 1;","tryCatchPattern":"try\n{\n    var ok = solver.Solve(matrix);\n}\ncatch (ArgumentException ex) when (ex.Message.Contains(\"n*(n+1)\"))\n{\n    // rebuild augmented matrix or reject input\n}","preventionTips":["Always append the constants column to form the n x (n+1) augmented matrix.","Validate shape right after parsing/loading the system.","Reject empty matrices before calling Solve."],"tags":["matrix","linear-algebra","argument-validation","csharp"],"backgroundTag":"tensor-shape-mismatch","analyzedSha":"96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c","analyzedAt":"2026-09-13T17:04:01.438Z","contentChangedAt":"2026-09-13T17:04:01.438Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}