Advertisement
dimipan80

Calculate N! / K!

Aug 8th, 2014
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.09 KB | None | 0 0
  1. // Write a program that calculates n! / k! for given n and k (1 < k < n < 100). Use only one loop.
  2.  
  3. import java.math.BigInteger;
  4. import java.util.Locale;
  5. import java.util.Scanner;
  6.  
  7. public class _06_CalculateN_K_FactorialsDivision {
  8.  
  9.     public static void main(String[] args) {
  10.         // TODO Auto-generated method stub
  11.         Locale.setDefault(Locale.ROOT);
  12.         Scanner scanner = new Scanner(System.in);
  13.         System.out.print("Enter a whole positive number in the range [3 .. 99] for N: ");
  14.         int numN = scanner.nextInt();
  15.         System.out.print("Enter other whole positive number in the range [2 .. N-1] for K: ");
  16.         int numK = scanner.nextInt();
  17.         scanner.close();
  18.  
  19.         if (numN < 100 && numN > numK && numK > 1) {
  20.             BigInteger factorialsDivision = BigInteger.ONE;
  21.             for (int i = numN; i > numK; i--) {
  22.                 BigInteger numBig = new BigInteger("" + i);
  23.                 factorialsDivision = factorialsDivision.multiply(numBig);
  24.             }
  25.  
  26.             System.out.println("The Result from Division of N! and K! factorials is: "
  27.                             + factorialsDivision);
  28.         } else {
  29.             System.out.println("Error! - Invalid Input!!!");
  30.         }
  31.     }
  32.  
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement