This document discusses calculating combinations (n choose r) through defining a factorial function in Java. It includes a flowchart and Java code to prompt the user for values of n and r, call the factorial function to calculate n!/(r!(n-r)!), and print out the combination result.
This document discusses calculating combinations (n choose r) through defining a factorial function in Java. It includes a flowchart and Java code to prompt the user for values of n and r, call the factorial function to calculate n!/(r!(n-r)!), and print out the combination result.
This document discusses calculating combinations (n choose r) through defining a factorial function in Java. It includes a flowchart and Java code to prompt the user for values of n and r, call the factorial function to calculate n!/(r!(n-r)!), and print out the combination result.
/** * Imprima la combinatoria de n tomados de a r. * * @author Jhonny Felipez * @version 1.0 27/04/2012 * */ public class Combinatoria {
public static long factorial(int n) { long f = 1; for (int i = 1; i <= n; i++) f = f * i; return f; }
public static void main(String[] args) { // Variables int n, r;
// Entrada Scanner lee = new Scanner(System.in); System.out.print("Ingrese n = "); n = lee.nextInt(); System.out.print("Ingrese r = "); r = lee.nextInt();
// Proceso y Salida System.out.printf("Combinatoria = %5.2f", (double) factorial(n) / (factorial(n - r) * factorial(r))); } }