Write a C# program to create a static function factorial and calculate the factorial of the number.
Object:
Write a program to explain the method in C#. Create a static function factorial() that accepts a number
from the user and returns the factorial of the number. C# Projects with Source Code
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace LAB_4a
{
class factorial
{
public static void fac()
{
int i, num, fact = 1;
Console.WriteLine(“Enter Any Number: “);
num = Convert.ToInt32(Console.ReadLine());
for (i = num; i >= 1; i–)
{
fact = fact * I;
}
Console.WriteLine(“\nFactorial of given Number is : ” + fact);
}
}
class Program
{
static void Main(string[] args)
{
factorial.fac();
Console.ReadKey();
}
}
}
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace LAB_4a { class factorial { public static void fac() { int i, num, fact = 1; Console.WriteLine("Enter Any Number: "); num = Convert.ToInt32(Console.ReadLine()); for (i = num; i >= 1; i--) { fact = fact * i; } Console.WriteLine("\nFactorial of given Number is : " + fact); } } class Program { static void Main(string[] args) { factorial.fac(); Console.ReadKey(); } } }
Output:
Another C# Program: C# Program Calculating the Area of a Rectangle
Another C# Program: C# Program that Subtracts two user-defined numbers
Another C# Program: C# program to count vowels in a string