-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLotteryOdds.java
More file actions
32 lines (25 loc) · 812 Bytes
/
LotteryOdds.java
File metadata and controls
32 lines (25 loc) · 812 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package Lottery;
import java.util.Scanner;
/**
* This program demonstrates a <code>for</code> loop,
*
* @author Cay Horstmann
* @version 1.20 2004-02-10
*/
public class LotteryOdds {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("How many numbers do you need to draw? ");
int k = in.nextInt();
System.out.print("What is the highest number you can draw? ");
int n = in.nextInt();
/*
* compute binomial coefficient n*(n-l)*(n-2)*...*(n-k+l)/(l*2*3*...*k)
*/
int lotteryOdds = 1;
for (int i = 1; i <= k; i++) {
lotteryOdds = lotteryOdds * (n - i + 1) / i;
}
System.out.println("Your odds are 1 in " + lotteryOdds + ". Good luck!");
}
}