I am practising on an online compiler. I have to write the factorial program that, given a number n from STDIN, it prints out the factorial of n to STDOUT:
I have written this
using System;
using System.Collections.Generic;
using System.IO;
class Solution {
static void Main(String[] args) {
int result=1;
int fact = int.Parse(args[0]);
if(fact==0)
{
Console.WriteLine(result);
}
else
{
for(int i=fact;i>=1;i--)
result = i*result;
}
Console.WriteLine(result);
}
}
I know in C, we use atoi
. I also tried using simple Console.ReadLine() but it simply fails the test case.
Currently, I am getting the following error -
Unhandled Exception:
System.IndexOutOfRangeException: Index was outside the bounds of the array.
at Solution.Main (System.String[] args) [0x00002] in <31af8dc2b3da4d24a38e31199d9b8949>:0
[ERROR] FATAL UNHANDLED EXCEPTION: System.IndexOutOfRangeException: Index was outside the bounds of the array.
at Solution.Main (System.String[] args) [0x00002] in <31af8dc2b3da4d24a38e31199d9b8949>:0