Write a C++ Program to find the factorial of a given number using class.

May 13, 2020

Question: Write a C++ Program to find the factorial of a given number using class.

Algorithm:

Program:

#include<iostream.h>
// define class
class factorial
{
    int i,f,n;
    public:
    void read_Number();
    void display_Factorial();
};

// define method read_Number
void factorial::read_Number()
{
    f=1;
    cout<<"\n Enter a number:";
    cin>>n;
    for(i=1;i<=n;i++)
    {
        f=f*i;
    }
}


// define method display_Factorial
void factorial::display_Factorial()
{
    cout<<"\n Factorial of "<<n<<" is "<<f;
}

// make object of class and call the mehtods
void main()
{
    factorial obj;
    obj.read_Number();
    obj.display_Factorial();
}

 

One comment on “Write a C++ Program to find the factorial of a given number using class.”

Leave a Reply

Your email address will not be published. Required fields are marked *

tech.techiya