Hello Everyone,

This program is designed to tell you the probability of winning the Lotto. I am having problem with using the recursive factorial function inside of the function computeWays. I am getting just the numbers of picks for the chance of winning. For example if I enter in 12 for how many numbers to pick I will get 12 for the winning chance of the lottery instead of 792. For the probability I am only getting a 0 when it is suppose to be 0.0013. How do I get the factorial to work??? And How do I get the probability to display more than zero.????

#include <iostream>

using namespace std;


int factorial(int num)
{
    if(num<=1)
        return 1;
    else
        return num*factorial(num-1);
}

int computeWay(int n, int k)
{
    int x = n-k;

    if(n>=k)

        return n;
    else if(n<k)
        return factorial(n)/(factorial(k) * factorial(x));
}



int main()
{
    int a;
    int b;

    cout<<"Enter a number from 1-12"<<endl;
    cin>>a;
    cout<<"Enter a number from 1-"<<a<<endl;
    cin>>b;

    int numOfWays=computeWay(a,b);
    int prob=1.0/numOfWays;

    cout<<"Your chance of winning is 1 out of"<<numOfWays<<endl;

    cout<<"This is a probability "<<prob<<endl;

    return 0;
}

Edited 7 Years Ago by WaltP: Corrected CODE tags -- with all the help about them, how could you miss using them???? And the PREVIEW button???

Hello Everyone,

This program is designed to tell you the probability of winning the Lotto. I am having problem with using the recursive factorial function inside of the function computeWays. I am getting just the numbers of picks for the chance of winning. For example if I enter in 12 for how many numbers to pick I will get 12 for the winning chance of the lottery instead of 792. For the probability I am only getting a 0 when it is suppose to be 0.0013. How do I get the factorial to work??? And How do I get the probability to display more than zero.????

#include <iostream>

using namespace std;


int factorial(int num)
{
    if(num<=1)
        return 1;
    else
        return num*factorial(num-1);
}

int computeWay(int n, int k)
{
    int x = n-k;

    if(n>=k)

        return n;
    else if(n<k)
        return factorial(n)/(factorial(k) * factorial(x));
}



int main()
{
    int a;
    int b;

    cout<<"Enter a number from 1-12"<<endl;
    cin>>a;
    cout<<"Enter a number from 1-"<<a<<endl;
    cin>>b;

    int numOfWays=computeWay(a,b);
    float prob=1.0/numOfWays;

    cout<<"Your chance of winning is 1 out of"<<numOfWays<<endl;

    cout<<"This is a probability "<<prob<<endl;

    return 0;
}

since you expect probability as your output...
you must be knowing that on the mother earth""probability<1""
so your output surely fractional hence floating point number eg. 0.45
since you are declaring it as int type o/p is rounded to '0' so declare your probability as float type and check...coz if you declare it as int you would either get it '0' or '1'
reply if problem not solved...

Edited 4 Years Ago by Reverend Jim: Fixed formatting

This article has been dead for over six months. Start a new discussion instead.