How to calculate factorial in C#

1 Answer

0 votes
using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int n = 5, fact = 1;

            try
            {
                for (int i = 1; i <= n; i++)
                     fact *= i;

                Console.WriteLine(fact);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
    }
}

/*
run:
   
120
 
*/ 


answered Apr 9, 2014 by avibootz
edited May 5, 2015 by avibootz

Related questions

2 answers 240 views
240 views asked Sep 29, 2023 by avibootz
2 answers 267 views
2 answers 360 views
1 answer 160 views
1 answer 213 views
213 views asked Jan 10, 2022 by avibootz
1 answer 188 views
188 views asked Jan 15, 2025 by avibootz
...