Factorial of a number------ using class....
Please give me the C++ code to find the factorial of a number using class....?
// Homework assignment by %26lt;insert your name here%26gt;
// cryptic? How can a recursive static member function with a ternary operator be in any way cryptic?
// Like the other guy said
// the return value strictly should be declared as int
// but my compiler assumes int is what is wanted.
#include %26lt;iostream%26gt;
using namespace std;
class CFact
{
public:
static int fact(int n)
{
return n?n*fact(n-1):1;
}
};
int main(int argc, char* argv[])
{
int val;
cout %26lt;%26lt; "Factorial! pls enter number" %26lt;%26lt; endl;
cin %26gt;%26gt; val;
val=CFact::fact(val);
cout %26lt;%26lt; "Factorial=" %26lt;%26lt; val;
}
Reply:Peter K, you forgot to specify the return type for the static function:
static int fact(int n)
I really like your answer, though...it's accurate, and so cryptic that the teacher will know it can't possibly have come from Kishore. :-)
Reply:I know this is your assignment....Please try to do it yourself..
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment