Answer to Question #297444 in C++ for Sonu

Question #297444

C++ program to find the factorial of a number using a constructor and destructor (generating the message "you have done it").

1
Expert's answer
2022-02-14T15:04:38-0500


#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;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog