Answer to Question #194471 in C++ for zaid

Question #194471

Write a C++ program which perform the following:


1.Create a Class factorial

2.Create Private Member Variable and initialize it with Member initialization list and with value zero

3.Now, take inputs from user

4.Create a Public Member Function Calculate( ) outside the class , to calculate the factorial of a given input

5.Create another Public Member Function Result( ) outside the class, to print the results on screen

6.Now, create a constant object of the class which perform following:

a. Passes the values to the Calculate( ) function

b. Call the Result( )

function to show result



1
Expert's answer
2021-05-17T13:29:43-0400
#include <iostream>
#include <string>


using namespace std;
//1.Create a Class factorial
class factorial
{
private:
	//2.Create Private Member Variable and initialize it with Member initialization list and with value zero
	int fact;
	int number;
public:


	factorial(){}
	//Destructor
	~factorial(){}
	//4.Create a Public Member Function Calculate( ) outside the class , to calculate the factorial of a given input
	void Calculate(int number)
	{
		this->number=number;
		fact=1;
		for(int i=1;i<=number;i++){    
			fact=fact*i;    
		}
	}
	//5.Create another Public Member Function Result( ) outside the class, to print the results on screen
	void Result()
	{
		cout<<number<<"! = "<<fact<<"\n\n";
	}
};


int main(void){
	//3.Now, take inputs from user
	int number;
	cout << "Enter the number: ";
	cin >> number;
	//a. Passes the values to the Calculate( ) function
	factorial fact;
	fact.Calculate(number);
	//b. Call the Result( ) function to show result
	fact.Result();
	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
APPROVED BY CLIENTS