Advertisement
Guest User

FactorialDivisionTsvetelin

a guest
Oct 19th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.58 KB | None | 0 0
  1.  
  2.  
  3. /*
  4.  * 19/46/2018 at 18:46:43
  5.  * FactorialDivision.java created by Tsvetelin
  6.  */
  7. package Methods_Excerices;
  8.  
  9. import java.math.BigDecimal;
  10. import java.math.BigInteger;
  11. import java.util.Scanner;
  12.  
  13. /**
  14.  * @author Tsvetelin
  15.  *
  16.  */
  17. public class FactorialDivision
  18. {
  19.  
  20.     /**
  21.      * @param args
  22.      */
  23.     public static void main ( String [] args )
  24.     {
  25.         Scanner in = new Scanner( System.in );
  26.        
  27.         int a = Integer.parseInt( in.nextLine() );
  28.         int b = Integer.parseInt( in.nextLine() );
  29.        
  30.         boolean positiveA = a>0; a = Math.abs( a );
  31.         boolean positiveB = b>0; b = Math.abs( b );
  32.        
  33.         BigInteger factA = positiveA
  34.                 ? factorial( new BigInteger( a + "" )  )
  35.                 : BigInteger.ZERO.subtract( factorial( new BigInteger( a + "" ) ) );
  36.         BigInteger factB = positiveB
  37.                 ? factorial( new BigInteger( b + "" ) )
  38.                 : BigInteger.ZERO.subtract( factorial( new BigInteger( b + "" ) ) );
  39.        
  40.         System.out.printf( "%.2f", divide( factA, factB ) );
  41.     }
  42.  
  43.     /**
  44.      * @param a
  45.      * @param b
  46.      * @return
  47.      */
  48.     private static BigDecimal divide ( BigInteger a , BigInteger b )
  49.     {
  50.            
  51.         return new BigDecimal( a.divide( b ) );
  52.     }
  53.  
  54.     /**
  55.      * @param a
  56.      * @return
  57.      */
  58.     private static BigInteger factorial ( BigInteger a )
  59.     {
  60.         if(a.compareTo( BigInteger.ZERO ) == 0) return BigInteger.ONE;
  61.         else return a.multiply( factorial( a.subtract( BigInteger.ONE ) ) );
  62.        
  63.     }
  64.  
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement