求助这道C#的loop题目要怎么写
来源:学生作业帮 编辑:搜搜考试网作业帮 分类:综合作业 时间:2024/08/18 03:36:39
求助这道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)
![求助这道C#的loop题目要怎么写](/uploads/image/z/5995546-34-6.jpg?t=%E6%B1%82%E5%8A%A9%E8%BF%99%E9%81%93C%23%E7%9A%84loop%E9%A2%98%E7%9B%AE%E8%A6%81%E6%80%8E%E4%B9%88%E5%86%99)
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;
}