C# Program to find factorial of a number using class and object
In this program, You will learn how to find factorial of a number using class and object in C#.
3! = 1 * 2 * 3 => 6
4! = 1 * 2 * 3 * 4 => 24
Example: How to find factorial of a number using class and object in C#.
using System;
public class Program
{
public int findFact(int n)
{
int i, f = 1;
for (i = 1; i <= n; i++)
{
f = f * i;
}
return f;
}
public static void Main(string[] args)
{
int x, f;
Console.Write("Enter a number:");
x = Convert.ToInt32(Console.ReadLine());
Program obj = new Program();
f = obj.findFact(x);
Console.WriteLine("Factorial is:" + f);
}
}
Output:
Enter a number:4
Factorial is:24