My programm does not calculate right (Java)

Nadezhda Sabeva :

I have to make a programm that calculate the probability of winning the lottery calculated in the Austrian system (6 out of 45).

I use this formula: 45!/6!*39! = 45 *44*43*42*41*40/1*2*3*4*5*6 (because the numbers are shortened). But it isn't calculated right and the programme gives 2179827 as an answer instead of 8415060. Can somebody help me and tell me where the mistake is?

package homework_1;

public class Aufgabe_3 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        int n = 45*44*43*42*41*40;
        int m = 1*2*3*4*5*6;
        int w = n/m;

        System.out.println("Die Wahrscheinlichkeit ist: " + w);
    }

}

Thank you very much in advance! :)

Rahul Agrawal :

In your code 45*44*43*42*41*40 it will exceed int limit. Use long to store the result

long n = (long)45*44*43*42*41*40;
long m = (long)1*2*3*4*5*6;
long w = n/m;

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=238572&siteId=1