desislava_topuzakova

08. Factorial Division

Oct 8th, 2021
1,275
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.97 KB | None | 0 0
  1. package Methods_Exercise;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class FactorialDivision_08 {
  6.     public static void main(String[] args) {
  7.         Scanner scanner = new Scanner(System.in);
  8.         int firstNumber = Integer.parseInt(scanner.nextLine());
  9.         int secondNumber = Integer.parseInt(scanner.nextLine());
  10.  
  11.         //факториел на първото число
  12.         long firstNumberFactorial = calculateFactorial(firstNumber);
  13.         //факториел на второто число
  14.         long secondNumberFactorial = calculateFactorial(secondNumber);
  15.         //факториел1 / факториел2 -> печатаме
  16.         double result = firstNumberFactorial * 1.0 / secondNumberFactorial;
  17.         System.out.printf("%.2f", result);
  18.     }
  19.  
  20.     private static long calculateFactorial (int number) {
  21.         long fact = 1;
  22.         for (int i = 1; i <= number ; i++) {
  23.             fact = fact * i;
  24.         }
  25.         return fact;
  26.     }
  27. }
  28.  
Advertisement
Add Comment
Please, Sign In to add comment