using System;
using System.Collections.Generic;
using System.Text;

namespace Factorial
{
    class Program
    {
        static void Main(string[] args)
        {
            int i, n, fac = 1;
            string s;
            Console.Write("Please enter any number to find it's factorial(0 to 19):\n");
            s = Console.ReadLine();
            n = Int32.Parse(s); 
            for (i = 1; i <= n; i++)
            {
                fac = fac * i;
            }
            Console.WriteLine("\nThe factorial of '{0}' is {1}.",n,fac);
            Console.Read();
        }
    }
}

INPUT/OUTPUT: 5/120

<snipped>

Dani AI

Generated

Good starter snippet from — a simple loop is the usual way to teach factorials. Two things to call out that the thread replies missed: missing input validation and silent numeric overflow. The original code uses a 32-bit int, so claims like “0 to 19” are misleading for that type.

Important numeric limits (exact):

  • Int32.MaxValue = 2,147,483,647 → largest safe factorial: 12! = 479,001,600. 13! = 6,227,020,800 overflows int.
  • Int64.MaxValue = 9,223,372,036,854,775,807 → largest safe factorial: 20! = 2,432,902,008,176,640,000. 21! overflows long.

Recommended approach for practical, safe results: validate input, handle negatives (factorial is defined only for non-negative integers), and use arbitrary-precision when you need >20!. Example using BigInteger (add using System.Numerics;):

using System;
using System.Numerics;

class Program
{
    static void Main()
    {
        Console.Write("Enter non-negative integer: ");
        if (!int.TryParse(Console.ReadLine(), out int n) || n < 0)
        {
            Console.WriteLine("Please enter a non-negative integer.");
            return;
        }

        BigInteger fact = BigInteger.One;
        for (int i = 2; i <= n; i++)
            fact *= i;

        Console.WriteLine("{0}! = {1}", n, fact);
    }
}

Troubleshooting tips:

  • If results look incorrect (negative or wrapped), you hit overflow — switch to long (up to 20!) or BigInteger.
  • Avoid recursion for very large n to prevent stack overflows; iterative multiplication is simpler and safer.
  • Computing very large factorials (thousands of digits) is CPU- and memory-intensive; for non-integer factorials look at Gamma-function implementations in numeric libraries.

This fixes the common pitfalls raised implicitly by and others, and keeps the code robust for real use.

Recommended Answers

All 5 Replies

Is this code supposed to mean something?

I think he is submitting this as a snippet...

c# syntax for factorial of the number
1.
using System;
2.
using System.Collections.Generic;
3.
using System.Text;
4.

5.
namespace Factorial
6.
{
7.
class Program
8.
{
9.
static void Main(string[] args)
10.
{
11.
double i, n ;
int fac = 1;
12.
string s;
13. Console.Write("Please enter any number to find it's factorial:\n");
14. s = Console.ReadLine();
15. n = Int32.Parse(s);
16. for (i = 1; i <= n; i++)
17. {
18. fac = fac * i;
19. }
20. Console.WriteLine("\nThe factorial of '{0}' is {1}.",n,fac);
21. Console.Read();
22. }
23. }
24. }

Allright... but this is horribly obvious code.

:ooh: Yeah, every morning the sun comes up.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.