Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Write a program that calculates n! / k! for given n and k (1 < k < n < 100). Use only one loop.
- import java.math.BigInteger;
- import java.util.Locale;
- import java.util.Scanner;
- public class _06_CalculateN_K_FactorialsDivision {
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- Locale.setDefault(Locale.ROOT);
- Scanner scanner = new Scanner(System.in);
- System.out.print("Enter a whole positive number in the range [3 .. 99] for N: ");
- int numN = scanner.nextInt();
- System.out.print("Enter other whole positive number in the range [2 .. N-1] for K: ");
- int numK = scanner.nextInt();
- scanner.close();
- if (numN < 100 && numN > numK && numK > 1) {
- BigInteger factorialsDivision = BigInteger.ONE;
- for (int i = numN; i > numK; i--) {
- BigInteger numBig = new BigInteger("" + i);
- factorialsDivision = factorialsDivision.multiply(numBig);
- }
- System.out.println("The Result from Division of N! and K! factorials is: "
- + factorialsDivision);
- } else {
- System.out.println("Error! - Invalid Input!!!");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement