Click here to Skip to main content
Click here to Skip to main content
Alternative Tip

Calculate the Factorial of an Integer in C#

By , 18 Oct 2011
Rate this:
Please Sign up or sign in to vote.
The System.Numeric.BigInteger class allows for calculating VERY LARGE values.
I created a sample window form app to calculate the factorial of an input value
private void button1_Click(object sender, EventArgs e)
    int inputValue;
    if (int.TryParse(this.textBox1.Text, out inputValue) && inputValue > 0)
        BigInteger bigInt = 1;
        while (inputValue > 1)
            bigInt = BigInteger.Multiply(inputValue--, bigInt);
        label1.Text = bigInt.ToString();
        label2.Text = bigInt.ToString().Length + " Digits";
        label1.Text = "Invalid Input";
        label2.Text = "";
just so you know, 1000! has 2568 digits Smile | :)


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

About the Author

Matthew Dennis
Software Developer (Senior) The Code Project
Canada Canada
Matthew works on improving the performance and experience of the Code Project site for users, clients, and administrators.
Matthew has more years of software develeopment, QA and architecture experience under his belt than he likes to admit. He gradutated for the University of Waterloo with a B.Sc. in Electrical Engineering. He started out developing micro-processor based hardware and software including compilers and operating systems.
His current focus is on .NET web development including jQuery, Webforms, MVC, AJAX, and patterns and practices for creating better websites.
He is the author of the Munq IOC, the fastest ASP.NET focused IOC Container.
His non-programming passions include golf, pool, curling, reading and building stuff for the house.
Follow on   Twitter

Comments and Discussions

GeneralMatthew, have a look at this site: PinmemberYvesDaoust18-Oct-11 22:35 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web03 | 2.8.140415.2 | Last Updated 18 Oct 2011
Article Copyright 2011 by Matthew Dennis
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid