Assignment: below is the program which calculates a factorial of a given positive number. If we pass a negative number it will generate an exception.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Exception_factorial_demo
{
class F1
{
void facto(int x)
{
if (x < 0)
throw new FormatException();
int f = 1;
while (x > 0)
{
f = f * x;
x--;
}
Console.WriteLine("factorial{0}", f);
}
class M {
static void Main(string[] args)
{
F1 f = new F1();
try
{
f.facto(4);
f.facto(-4);
f.facto(3);
}
catch(Exception ex)
{
Console.WriteLine("Error", ex.Message);
}
Console.ReadLine();
}
}
}
}
Comments
Post a Comment