I'm trying to create a code that gives the factorial for a number given. If it is negative, string, or to large of a number an error should return. I figured out how to do the catch for the negative and string, but I'm confused on how to create a catch for a larger number. The teacher gave us this example but it really doesn't help. long f = 0; f = checked (a * b); But I don't know which variables or what to do. Any help would be appreciated.

using System;

public class Factorial
{
   public static void Main(string[] args)
   {
      bool continueLoop = true;

      do
      {
         try
         {
            Console.Write("Enter a non-negative integer: ");
            int number = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("{0}! = {1}", number, factorial(number));
            continueLoop = false;
         }
         catch ( FormatException)
         {
            Console.WriteLine("Invalid number format.  Please try again.\n");
         }
         catch (NegativeNumberException negativeNumberException)
         {
            Console.WriteLine(negativeNumberException.Message);
         }
         catch (OverflowException)
         {
            int result = 0;
            result = checked();
            Console.WriteLine("Input value too large.  Please try again.");
         }
      } while (continueLoop);

      Console.ReadKey();
   }// end Main

      public static int factorial (int value)
      {
         if (value < 0)
            throw new NegativeNumberException("Factorial of negative integer not permitted.  Please try again\n");
         else
            return Factor(value); 
      }

       public static int Factor(int number)
       {
          int result = 1;
            for (int x = 1; x <= number; x++)
            {
               result *= x;
            }
          return result;
       }









} // end class Factorial

Given that an int can hold a maximum value of 2,147,483,647 the maximum number that could be entered is 12 as 13 gives an overflow. Your example uses long so maybe you should be using that too.

I'm not a mathmatician but I'm not sure what your instructor's check code would achieve. A times B is not the same as A!.

I'm not sure how to write the code.

long result = 0;
result = checked(12)
Console.WriteLine("Input value too large.  Please try again");

?? I tried that but it didn't work

Edited 2 Years Ago by theashman88

what exactly are you trying to do. The fact that it is in the overflow exception means it has exceeded its available limit. changing the datatype here won't help since the variable "result" is created inside "Factor".

checked (C# Reference)

"...By default, an expression that contains only constant values causes a compiler error if the expression produces a value that is outside the range of the destination type. If the expression contains one or more non-constant values, the compiler does not detect the overflow..."

"...By default, these non-constant expressions are not checked for overflow at run time..."

...Overflow checking can be enabled by compiler options, environment configuration, or use of the checked keyword..."

public static long Factor(int number)
{
    long result = 1;

    try
    {
        //enables overflow checking
        checked
        {
            for (int x = 1; x <= number; x++)
            {

                result *= x;

            }//for
        }//checked

    }//try
    catch (OverflowException ex)
    {
        //Console.WriteLine("Error: " + ex.Message);
        throw new OverflowException();
    }//catch

    return result;
}//Factor

20! = 2,432,902,008,176,640,000
21! = 51,090,942,171,709,440,000

Max long: 9,223,372,036,854,775,807

Or in scientific (exponential) notation:

20! = 2.432902008176640E+18
21! = 51.090942171709440E+18

Max long: 9.223372036854775807E+18

*Note: I put all numbers above in E+18 for easier comparision.

Using 'long' data type:
21! will throw an OverflowException
providing that you have enabled overflow checking.

Other resources:
Arithmetic Overflow Checking using checked/unchecked

Edited 2 Years Ago by cgeier

This question has already been answered. Start a new discussion instead.