求助这道C#的loop题目要怎么写
来源:学生作业帮 编辑:神马作文网作业帮 分类:综合作业 时间:2024/11/11 05:39:59
求助这道C#的loop题目要怎么写
Write a C# program to find the factorial of a number. You are required to
• Accept a positive integer number from the user at run time (2 Marks)
• Use exception handling technique to avoid format problems (5 Marks)
• Use loops (3 Marks)
You are free to use recursion technique or simple method or property. (5 Marks)
Write a C# program to find the factorial of a number. You are required to
• Accept a positive integer number from the user at run time (2 Marks)
• Use exception handling technique to avoid format problems (5 Marks)
• Use loops (3 Marks)
You are free to use recursion technique or simple method or property. (5 Marks)
static void Main(string[] args)
{
ulong input = 0;
while (input<=0 || input>65)
{
Console.WriteLine();
Console.Write("input a number between 0 and 65:");
try
{
input = ulong.Parse(Console.ReadLine());
if (input > 65) throw new Exception();
}
catch
{
Console.WriteLine("error format! input again.");
}
}
Console.WriteLine("factorial of {0} is {1}.", input, fact(input));
Console.ReadKey();
}
static ulong fact(ulong i)
{
if (i <= 1) return 1;
return fact(i - 1)*i;
}
{
ulong input = 0;
while (input<=0 || input>65)
{
Console.WriteLine();
Console.Write("input a number between 0 and 65:");
try
{
input = ulong.Parse(Console.ReadLine());
if (input > 65) throw new Exception();
}
catch
{
Console.WriteLine("error format! input again.");
}
}
Console.WriteLine("factorial of {0} is {1}.", input, fact(input));
Console.ReadKey();
}
static ulong fact(ulong i)
{
if (i <= 1) return 1;
return fact(i - 1)*i;
}