Follow on Facebook

Like and Share on Facebook

Tuesday, March 10, 2015

Write a Program to Print Factorial of a Given Number Using Exception Handling

import java.io.*;

class  Factorial
{
    public static void main(String args[])
    {
       
         int no,fact;
         fact=1;

         try{
            BufferedReader obj = new BufferedReader(new InputStreamReader(System.in));
            System.out.print("Enter number whose Factorial is to be found : ");
            no=Integer.parseInt(obj.readLine());

                   while(no > 0)
                {
                      fact=fact*no;
                      no=no-1;
                }

            System.out.println("FACTORIAL of a given number is  :  "+fact);
            }
            catch(Exception e)
            {}
    }
}


Output:
Enter number whose Factorial is to be found : 12
FACTORIAL of a given number is  :  479001600

No comments:

Post a Comment