What's new

C++ Class get factorial

Welcome Guest

Eternal Poster
Joined
Nov 29, 2015
Posts
1,000
Reaction
146
Points
509
Age
27
C++:
#include <iostream>
using namespace std;


class Factorial
{
    public:
        Factorial();
        void setNum(int); // setter function that will set the value of the member variable to the value passed to the class
        int getNum(); //gets/access the content of the member variable
        int solveFactorial();

    private:
        int num;
};

Factorial::Factorial()
{
    num=0;
}
void Factorial::setNum(int n)
{
    num=n;
}
int Factorial::getNum()
{
    return num;
}
int Factorial::solveFactorial()
{
    int prod=1;
    for (int i=1; i<=num; i++)
    {
        prod=prod*i;
    }

    return prod;
}
int main()
{
    int n;

    cout << "Enter a number: ";
    cin >> n;

    Factorial f;
    f.solveFactorial();

}

Mga boss, bat ayaw po lumabas ng output or result tuwing e run ko siya? Example input is 5, supposedly dapat 120 ang result. Ang lumalabas sakin ay blank or null.
 

Similar threads

Back
Top