Factorial of a number using Operator Overloading cpp
In this program, we are going see how to find Factorial of a number using Function Overloading  in C++ Programming Language.



The Code given below can be used in TURBO C++ Compilers: -

#include <iostream.h>
#include <conio.h>

class Factorial
{
    long int fact;

    public:
    Factorial(int f = 0)
    {
        fact = f;
    }
    void showfact()
    {
        cout << "Factorial: " << fact << endl;
    }
    void operator*=(int iter)
    {
        fact = fact * iter;
    }
};

void main()
{
    clrscr();
    int num;
    cout << "Enter number: ";
    cin >> num;
    Factorial f(num);
    for (int i = num - 1; i > 0; i--)
    {
        f *= i;
    }
    f.showfact();

    getch();
}

//.......Coded by SAHIL SHAIKH

The Code given below can be used in gcc/g++ Compilers: -

#include <iostream>
using namespace std;

class Factorial
{
    long int fact;

    public:
    Factorial(int f = 0)
    {
        fact = f;
    }
    void showfact()
    {
        cout << "Factorial: " << fact << endl;
    }
    void operator*=(int iter)
    {
        fact = fact * iter;
    }
};

int main()
{
    int num;
    cout << "Enter number: ";
    cin >> num;
    Factorial f(num);
    for (int i = num - 1; i > 0; i--)
    {
        f *= i;
    }
    f.showfact();

    return 0;
}

//.......Coded by SAHIL SHAIKH

#ENJOY CODING

Post a Comment

FOR ANY DOUBTS AND ERRORS FEEL FREE TO ASK. YOUR DOUBTS WILL BE ADDRESSED ASAP

Previous Post Next Post