C++ program to find factorial of a no. by defining the member functions outside the class
C++ program to find the factorial of a no. by defining the member functions outside the class.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | #include<iostream> using namespace std; class T4Tutorials_Factorial_Number { private: int n,n1,f=1; public: void input(); void calc(); void display(); }; void T4Tutorials_Factorial_Number::input() { cout<<"Please Enter a no.:”<<endl; cin>>n; } void T4Tutorials_Factorial_Number::calc() { n1=n; if(n==0||n==1) cout<<" Factorial of "<<n<<" is 1”<<endl; else { while(n> 0) { f=f*n; n--; } } } void T4Tutorials_Factorial_Number::display() { cout<<" The Factorial of "<<n1<<" is "<<f; } int main () { T4Tutorials_Factorial_Number object; object.input(); object.calc(); object.display(); } |
Output
Please Enter a no.: 4
The Factorial of 4 is 24
More Practice on Factorial problem in C++
- Factorial Program in C++
- factorial using single inheritance
- Factorial Program in C++ using Class Objects
- factorial using Multiple inheritances
C++ program for factorial using Constructor DestructorFactorial Of A Number By Using The Recursion - Factorial Program with structures and pointers C++
- Factorial Program with Nested Structure C++
- factorial of a no. by defining the member functions outside the class