8.Define a method to find factorial of a Given Number
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace factoriald
{
class Program
{
public static void Fact(int num)
{
int i, factorial = 1;
for (i = 1; i <= num; i++)
{
factorial = factorial * i;
}
Console.WriteLine("FACTORIAL= " + factorial);
}
static void Main(string[] args)
{
int num;
Console.WriteLine("ENTER THE NUMBER");
num = int.Parse(Console.ReadLine());
if (num < 0)
Console.WriteLine("Factorial not possible");
else if (num == 0)
Console.WriteLine("Factorial is 1");
else
Fact(num);
}
}
}
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace factoriald
{
class Program
{
public static void Fact(int num)
{
int i, factorial = 1;
for (i = 1; i <= num; i++)
{
factorial = factorial * i;
}
Console.WriteLine("FACTORIAL= " + factorial);
}
static void Main(string[] args)
{
int num;
Console.WriteLine("ENTER THE NUMBER");
num = int.Parse(Console.ReadLine());
if (num < 0)
Console.WriteLine("Factorial not possible");
else if (num == 0)
Console.WriteLine("Factorial is 1");
else
Fact(num);
}
}
}
No comments:
Post a Comment