Jumat, 08 Juli 2011

To calculate factorial of a given number using copy constructor.


ALGORITHM:
STEP 1: Start the program.
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.
STEP 10: Stop the program.

PROGRAM:
01#include<iostream.h>
02#include<conio.h>
03class copy
04{
05              int var,fact;
06              public:
07 
08                copy(int temp)
09                {
10                 var = temp;
11                }
12 
13                double calculate()
14                {
15                            fact=1;
16                            for(int i=1;i<=var;i++)
17                            {
18                            fact = fact * i;
19                            }
20                            return fact;
21                }
22};
23void main()
24{
25    clrscr();
26    int n;
27    cout<<"\n\tEnter the Number : ";
28    cin>>n;
29    copy obj(n);
30    copy cpy=obj;
31    cout<<"\n\t"<<n<<" Factorial is:"<<obj.calculate();
32    cout<<"\n\t"<<n<<" Factorial is:"<<cpy.calculate();
33    getch();
34}
Output:
Enter the Number: 5
Factorial is: 120
Factorial is: 120

Tidak ada komentar:

Posting Komentar