Write a C++ program to calculate factorial of a given number using copy constructor.

Program Algorithm:
Step 1: Include the header files
Step 2: Declare the class name as copy with data members and member functions.
Step 3: The constructor copy () with argument to assign the value.
Step 4: To cal the function calculate () do the following steps.
Step 5: for i=1 to var do
Step 6: Calculate fact*i to assign to fact.
Step 7: Increment the value as 1.
Step 8: Return the value fact.
Step 9: Print the result.




Program Code in C++:
#include<iostream.h>
#include<conio.h>
class copy
{
int var,fact;
public:
copy(int temp)
{
var = temp;
}
double calculate()
{
fact=1;
for(int i=1;i<=var;i++)
{
fact = fact * i;
}
return fact;
}
};
void main()
{
clrscr();
int n;
cout<<"\n\tEnter the Number : ";
cin>>n;
copy obj(n);
copy cpy=obj;
cout<<"\n\t"<<n<<" Factorial is:"<<obj.calculate();
cout<<"\n\t"<<n<<" Factorial is:"<<cpy.calculate();
getch();
}





Program Output:
Enter the Number: 5
Factorial is: 120
Factorial is: 120
Mukesh Rajput

Mukesh Rajput

I am a Computer Engineer, a small amount of the programming tips as it’s my hobby, I love to travel and meet people so little about travel, a fashion lover and love to eat food, I am investing a good time to keep the body fit so little about fitness also..

Post A Comment:

0 comments: