factorial function in C++ by R4R Team


program:

#include < iostream>
using namespace std;
class myclass
{
public:
//Factorial function
int fact(int n)
{
int i,ans=1;
for(i=n;i>0;i--)
{
ans=ans*i;
}
return ans;
}
};
int main()
{
int n,ans;
myclass obj;
cout<<"Enter any numbern";
cin>>n;
ans=obj.fact(n);
cout<<"Factorial is: "<< ans;
return 0;
}


output-

Enter any number
5
Factorial is: 120




Leave a Comment:
Search
Categories
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!