Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*program to calculate the factorial of a number */
- #include <iostream>
- using namespace std;
- class factorial
- {
- private:
- long int fact,number;
- public:
- void getdata()
- {
- wrong:
- cout<<"\nENTER A POSITIVE INTEGER : ";
- cin>>number;
- while(number<0)
- {
- cout<<"WRONG INPUT!";
- cout<<"\nENTER A POSITIVE INTEGER : ";
- cin>>number;
- }
- fact=1;
- }
- void calculate()
- {
- int i=1;
- if(number==0 || number==1)
- {
- cout<<"\nTHE FACTORIAL OF "<<number<<" IS : 1";
- }
- else
- {
- for(i=1;i<=number;i++)
- {
- fact=fact*i;
- }
- cout<<"\nTHE FACTORIAL OF "<<number<<" IS : "<<fact;
- }
- }
- };
- int main()
- {
- factorial f1;
- int cont=0;
- do
- {
- f1.getdata();
- f1.calculate();
- cout<<"\nDO YOU WANT TO CHECK AGAIN (1/0):";
- cin>>cont;
- }while(cont==1);
- return 0;
- }
- /***************************OUTPUT*************************************
- ENTER A POSITIVE INTEGER : 5
- THE FACTORIAL OF 5 IS : 120
- DO YOU WANT TO CHECK AGAIN (1/0):1
- ENTER A POSITIVE INTEGER : 12
- THE FACTORIAL OF 12 IS : 479001600
- DO YOU WANT TO CHECK AGAIN (1/0):0
- Process returned 0 (0x0)
- Press any key to continue.
- */
Add Comment
Please, Sign In to add comment