From 4b71f728751cb2bfccd13908a74791238fa11a8c Mon Sep 17 00:00:00 2001 From: newtykins Date: Tue, 21 Dec 2021 20:16:51 +0000 Subject: [PATCH] nCr in Java --- .gitignore | 1 + .vscode/settings.json | 3 +++ c/algorithms/binary-search.c | 0 c/calculators/helpers/factorial.c | 9 ++++++++ c/calculators/ncr.c | 11 +--------- java/calculators/ncr.java | 35 +++++++++++++++++++++++++++++++ readme.md | 5 +++-- 7 files changed, 52 insertions(+), 12 deletions(-) create mode 100644 .vscode/settings.json create mode 100644 c/algorithms/binary-search.c create mode 100644 c/calculators/helpers/factorial.c create mode 100644 java/calculators/ncr.java diff --git a/.gitignore b/.gitignore index b3e19ca..d4ee4ba 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ venv private __pycache__ +*.out diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..507bb42 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "java.project.sourcePaths": ["java"] +} diff --git a/c/algorithms/binary-search.c b/c/algorithms/binary-search.c new file mode 100644 index 0000000..e69de29 diff --git a/c/calculators/helpers/factorial.c b/c/calculators/helpers/factorial.c new file mode 100644 index 0000000..68db1e3 --- /dev/null +++ b/c/calculators/helpers/factorial.c @@ -0,0 +1,9 @@ +int factorial(int n) { + int ans = 1; + + for (int i = 2; i <= n; i++) { + ans *= i; + } + + return ans; +} diff --git a/c/calculators/ncr.c b/c/calculators/ncr.c index 5390fcb..aee367c 100644 --- a/c/calculators/ncr.c +++ b/c/calculators/ncr.c @@ -1,14 +1,5 @@ #include - -int factorial(int n) { - int ans = 1; - - for (int i = 2; i <= n; i++) { - ans *= i; - } - - return ans; -} +#include "helpers/factorial.c" int nCr(int n, int r) { return factorial(n) / factorial(r) * factorial(n - r); diff --git a/java/calculators/ncr.java b/java/calculators/ncr.java new file mode 100644 index 0000000..a5e4d57 --- /dev/null +++ b/java/calculators/ncr.java @@ -0,0 +1,35 @@ +package calculators; +import java.util.Scanner; + +class CombinationCalculator { + private static int factorial(int n) { + int res = 1; + + for (int i = 2; i <= n; i++) { + res *= i; + } + + return res; + } + + private static int nCr(int n, int r) { + return factorial(n) / (factorial(r) * factorial(n - r)); + } + + public static void main(String[] args) { + // Open a scanner and take the relevant inputs + Scanner scan = new Scanner(System.in); + + System.out.print("Please input the value for n: "); + int n = scan.nextInt(); + + System.out.print("Please input the value for r: "); + int r = scan.nextInt(); + + // Close the scanner, and calculate the result + scan.close(); + + int result = nCr(n, r); + System.out.println(result); + } +} diff --git a/readme.md b/readme.md index c17f9e7..0f2fbc5 100644 --- a/readme.md +++ b/readme.md @@ -7,8 +7,9 @@ ### Table of contents -- [Python](python) -- [C](c) +- [Python](languages/python) +- [C](languages/c) +- [Java](languages/java) - [Project Euler](euler) - [GCSE Computer Science](school/gcse) - [Royal Institute Computer Science Masterclass 2021](school/royal%20institute)