Advertisement
Nalakava

Factorial

Nov 9th, 2017
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.24 KB | None | 0 0
  1. import java.io.File;
  2. import java.io.FileWriter;
  3. import java.io.IOException;
  4. import java.math.BigInteger;
  5. import java.util.Scanner;
  6.  
  7. public class FactorsOfFactorial {
  8.  
  9. public static void main(String[] args)
  10. {
  11. while(true)
  12. {
  13. BigInteger n = BigInteger.valueOf(new Scanner(System.in).nextInt());
  14. factorial(n);
  15. }
  16. }
  17.  
  18. public static BigInteger recursiveFactorial(BigInteger n)
  19. {
  20. if(n.equals(BigInteger.ONE))
  21. return BigInteger.ONE;
  22.  
  23.  
  24. return n.multiply(recursiveFactorial(n.subtract(BigInteger.ONE)));
  25. }
  26.  
  27. public static void factorial(BigInteger n)
  28. {
  29. BigInteger val = BigInteger.ONE;
  30. for(BigInteger i = n; i.compareTo(BigInteger.ONE) == 1; i = i.subtract(BigInteger.ONE))
  31. {
  32. val = val.multiply(i);
  33. System.out.println("i: " + i);
  34. }
  35.  
  36. File f = new File("fact1.out");
  37. try {
  38. f.createNewFile();
  39. } catch (IOException e1) {
  40. // TODO Auto-generated catch block
  41. e1.printStackTrace();
  42. }
  43.  
  44. FileWriter scan;
  45. try {
  46. scan = new FileWriter(f);
  47. scan.write(val.toString());
  48. scan.close();
  49. System.out.println("Printed");
  50. } catch (IOException e) {
  51. // TODO Auto-generated catch block
  52. e.printStackTrace();
  53. }
  54. }
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement