Write a C# program to create a static function factorial and calculate factorial of the number.
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();
}
}
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | 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