Ad

Wednesday, August 14, 2013

Program To Find a Factorial of a Given Number


Overview:                  In this article i would like to publish a Program to Find a Factorial of a Given Numbers using C#.Net.


Description:

                     Factorial function specifies that it multiplies the given number with difference of a number with1 is nothing but a factorial of a given number.

                                             F=(n-1)*(n-2)*(n-3).............................


E.G:        
                5!=5*4*3*2*1
Program:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;



namespace Factorial
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Program To Find a Factorial of a given Number");
            int n;
            long f = 1; //Because if you give big values it can accomidates
            Console.WriteLine("Enter a Number");
            n = Convert.ToInt32(Console.ReadLine());
            for (int i = 1; i < n; i++)
            {
                f += f * i;
            }
            Console.WriteLine("Factorial of {0} is:{1}",n,f);
            Console.WriteLine("Keep Visiting http://www.pv999.com\nShare Your viewes and reviewes.....\nThanks for visiting....");
            Console.ReadLine();
        }
    }
}


Factorial in C#.net
              
Program On While Loop:

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

namespace Factorial
{
    class Class1
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Program To Find a Factorial of a given Number");
            int n,i=1;
            long f = 1; //Because if you give big values it can accomidates
            Console.WriteLine("Enter a Number");
            n = Convert.ToInt32(Console.ReadLine());
            while(i
            {
            f += f * i;
            i++;
            }
            Console.WriteLine("Factorial of {0} is:{1}", n, f);
            Console.WriteLine("Keep Visiting http://www.pv999.com\nShare Your viewes and reviewes.....\nThanks for visiting....");
            Console.ReadLine();
        }
    }
}


Output:
Factorial in C#




No comments: