20. Write a program to find factorial of a given number. Throw multiple exceptions and define multiple catch statements to handle negative number andout of memory exception. Negative number exception thrown if given number is negative value and out of memory exception is thrown if the given number is greater than 20

 

/* FACTORIAL USING MULTIPLE CATCH STATEMENTS */



# include<iostream>
using namespace std;
 
int main()
    {
        int n,i,f=1;
        cout<<"\n enter a number";
        cin>>n;
        try
        {
            if(n>0&&n<20)
            {
                for(i=1;i<n+1;i++)
                f=f*i;
            cout<<"the factorial is"<<f;
            }
        else if(n<0)
            throw (0);
        if(n>20)
            throw(1.0);
        }
        
catch(int i)
    {
        cout<<"\n you have enterd a - number";
    }

catch(double i) 
    {
        cout<<"\n it is out of memory";
    }
}

Posted by Unknown On 04:58 No comments

0 comments:

Post a Comment

  • RSS
  • Delicious
  • Digg
  • Facebook
  • Twitter
  • Linkedin
  • Youtube

Blog Archive

Contact Us


Name

E-mail *

Message *