Thread: factorial(newbie Q,please heeellp)

  1. #1
    Unregistered
    Guest

    Unhappy factorial(newbie Q,please heeellp)

    Hi,


    I am at beginning stage of VC++,and stumped on this question.Find the total of number of factorials ,user inputs.
    For eg. user says 5,

    ans = 1!+2!+3!+4!+5!

    I have done this,but confused where I am going wrong....

    #include<iostream>
    using std::cin;
    using std::cout;
    using std::endl;

    int main()

    {
    int num,ans,x,y,answer;
    ans=1;
    answer=0;


    cout<<"enter any integer"<<endl;
    cin>>num;

    if (num<0)
    {
    cout<<"u have entered nonnegative integer"<<endl;
    }
    else if (num==0)
    {
    cout<<"the"<<num<<"factorial is"<<"1"<<endl;
    }
    else if (num>0)
    {
    for(y=1;y<=num;y++)
    {
    for(x=y;x>=1;x--)
    {
    ans=x*ans;
    }
    }

    answer=answer+ans;
    cout<<"the value of mathematical constant is:"<<answer<<endl;

    }
    return 0;
    }

    I will be really grateful,if anybody points me out the right track.
    Thankx

  2. #2
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    You placed "answer = answer + ans" on the wrong place. And you also forget to reset "ans" in the first for-loop. It should be:

    #include<iostream>
    using std::cin;
    using std::cout;
    using std::endl;

    int main()

    {
    int num,ans,x,y,answer;
    ans=1;
    answer=0;


    cout<<"enter any integer"<<endl;
    cin>>num;

    if (num<0)
    {
    cout<<"u have entered nonnegative integer"<<endl;
    }
    else if (num==0)
    {
    cout<<"the"<<num<<"factorial is"<<"1"<<endl;
    }
    else if (num>0)
    {
    for(y=1;y<=num;y++)
    {
    ans = 1;

    for(x=y;x>=1;x--)
    {
    ans=x*ans;
    }

    answer=answer+ans;
    }

    cout<<"the value of mathematical constant is:"<<answer<<endl;
    }
    return 0;
    }

    Some programming tips:

    1. When doing calculations like A = A + B, in C++ you can reduce this to A += B.

    2. I would use other names than ans and answer. In you program the variable ans represents the factorial of the current y value. So you could rename ans to factorial. Also the variable answer could be renamed to something like sum. That makes the code more readible.

    3. A debugger is a powerful tool. A different easy way to debug your code, which is also very powerful, is the use of cout's or printf's to print certain variables on the screen. In that way you can analyse very quickly what's going wrong. For example:

    if (action_completed == TRUE)
    {
    cout << "the action is completed";
    // some other code
    }
    else
    {
    cout << "the action was not completed";
    // some other code
    }

  3. #3
    Unregistered
    Guest
    Here try this:


    int fact(int n)
    {
    if(n == 1)
    return n;
    return n * fact(n-1);
    }

  4. #4

    Post !

    #include <iostream>
    using namespace std;

    typedef unsigned int uint;


    uint factorial(uint num);


    int main()
    {
    cout << factorial(5) << endl;

    return 0;
    }



    uint factorial(uint num)
    {
    /* Factorial of one is one -- Thus we'll return one /*
    if(num == 1)
    return 1;

    else
    return num * factorial(num - 1); /* Otherwise it's num * the factorial of (num - 1) /*
    }
    Last edited by unanimous; 12-29-2001 at 12:52 PM.

  5. #5
    Unregistered
    Guest

    Thumbs up thankx

    thanks a lot....
    May god bless u all...

Popular pages Recent additions subscribe to a feed