C++ program to find the factorial of a number using a constructor and destructor (generating the message "you have done it").
#include <iostream>
#include <string>
using namespace std;
class Factorial{
public:
Factorial(int n){
long double factorial = 1.0;
if (n < 0)
cout << "Error! Factorial of a negative number doesn't exist.";
else {
for(int i = 1; i <= n; ++i) {
factorial *= i;
}
cout << "Factorial of " << n << " = " << factorial;
}
}
~Factorial(){
cout<<"\n\nyou have done it\n\n";
}
};
int main() {
int n;
cout << "Enter a positive integer: ";
cin >> n;
Factorial* factorial=new Factorial(n);
delete factorial;
system("pause");
return 0;
}
Comments
Leave a comment