m2skills

factorial cpp

May 31st, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.44 KB | None | 0 0
  1. /*program to calculate the factorial of a number */
  2.  
  3. #include <iostream>
  4.  
  5. using namespace std;
  6. class factorial
  7. {
  8.     private:
  9.         long int fact,number;
  10.     public:
  11.         void getdata()
  12.         {
  13.             wrong:
  14.             cout<<"\nENTER A POSITIVE INTEGER : ";
  15.             cin>>number;
  16.             while(number<0)
  17.             {
  18.                 cout<<"WRONG INPUT!";
  19.                 cout<<"\nENTER A POSITIVE INTEGER : ";
  20.                 cin>>number;
  21.             }
  22.             fact=1;
  23.         }
  24.         void calculate()
  25.         {
  26.             int i=1;
  27.             if(number==0 || number==1)
  28.             {
  29.                 cout<<"\nTHE FACTORIAL OF "<<number<<" IS : 1";
  30.             }
  31.             else
  32.             {
  33.                 for(i=1;i<=number;i++)
  34.                 {
  35.                     fact=fact*i;
  36.                 }
  37.                 cout<<"\nTHE FACTORIAL OF "<<number<<" IS : "<<fact;
  38.             }
  39.         }
  40. };
  41.  
  42. int main()
  43. {
  44.     factorial f1;
  45.     int cont=0;
  46.     do
  47.     {
  48.         f1.getdata();
  49.         f1.calculate();
  50.         cout<<"\nDO YOU WANT TO CHECK AGAIN (1/0):";
  51.         cin>>cont;
  52.     }while(cont==1);
  53.     return 0;
  54. }
  55.  
  56. /***************************OUTPUT*************************************
  57.  
  58. ENTER A POSITIVE INTEGER : 5
  59.  
  60. THE FACTORIAL OF 5 IS : 120
  61. DO YOU WANT TO CHECK AGAIN (1/0):1
  62.  
  63. ENTER A POSITIVE INTEGER : 12
  64.  
  65. THE FACTORIAL OF 12 IS : 479001600
  66. DO YOU WANT TO CHECK AGAIN (1/0):0
  67.  
  68. Process returned 0 (0x0)
  69. Press any key to continue.
  70. */
Add Comment
Please, Sign In to add comment