Advertisement
dimipan80

Trailing Zeroes in Nth Factorial

Aug 9th, 2014
284
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.77 KB | None | 0 0
  1. /* Write a program that calculates with how many zeroes the factorial
  2.  * of a given number n has at its end.
  3.  * Your program should work well for very big numbers, e.g. n=100000. */
  4.  
  5. import java.util.Scanner;
  6.  
  7. public class _18_TrailingZeroesInN_Factorial {
  8.  
  9.     public static void main(String[] args) {
  10.         // TODO Auto-generated method stub
  11.         Scanner scan = new Scanner(System.in);
  12.         System.out.print("Enter a positive Integer number: ");
  13.         int numN = scan.nextInt();
  14.         scan.close();
  15.  
  16.         if (numN > 0) {
  17.             int countZeroes = 0;
  18.             while (numN > 4) {
  19.                 numN /= 5;
  20.                 countZeroes += numN;
  21.             }
  22.  
  23.             System.out.printf("The Count Zeroes in that N-Factorial is: %d !\n",
  24.                     countZeroes);
  25.         } else {
  26.             System.out.println("Error! - Invalid Input number!!!");
  27.         }
  28.     }
  29.  
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement