vendredi 2 octobre 2020

Why does my variable sannolikhet only print out zero?

I am trying to create a program that counts the probability out of n people that atleast 2 persons have the same birthday.

int main()
{


int n;
double sannolikhet = 1;

cout << "Skriv in antal personer mellan 2 - 182: ";
cin >> n;  

Here I ask the user to write in the amount of people between the numbers 2 - 182.

while (n < 2 || n > 182)
{
    cout << "OBS! Skriv in ett tal mellan 2 - 182: \n";
    cout << "Skriv in antal personer mellan 2 - 182: ";
    cin >> n;
}

Here is the problem, every time i changes I want it to store the previous calculation in sannolikhet. I want the for loop to count like this "365/365 * 364/365 * 363/365 ... (366-n)/365". When it prints out sannolikhet in the end it just shows zero.

for (int i = 1; i < n; i++)
{
    sannolikhet = sannolikhet * ((366 - i) / 365); 
}



cout << "Sannolikheten att två personer har samma födelsedag är: " << fixed << setprecision(7) << sannolikhet <<" procent";


return 0;

}

Aucun commentaire:

Enregistrer un commentaire