E Factorial Divisibility
selected by Joe Horton
The number (n!) is de ned to be the product of the n
smallest positive integers. Thus 4! = 1 × 2 × 3 × 4.
We want to know how many times you can divide 2 into (n!).
Since 2 divides 12 once, and 2 divides 4 twice,
but does not divide 3 at all, the number of times 2 divides
(4!) is exactly three. Write a program to determine
the number of times n! is divisible by 2.
Input
A sequence of positive integers all on one line, separated
by spaces, and followed by 0.
Output
For each input integer n, excluding the last zero, print out a line: ”n factorial is divisible by exactly k 2’s.”
Sample input
5 7 2 0
Output should be:
5 factorial is divisible by exactly 3 twos.
7 factorial is divisible by exactly 4 twos.
2 factorial is divisible by exactly 1 twos.
(Ignor the bad grammar in the special case when the number of twos is 1.)

I am just a beginner in Java programming.
I looked up your int code for the factorial and compared it to an excel sheet output. I looks like your coding is missing the fact that the system will always start at zero even if your loop says i=1.
With you code, the factorial of 5 for example is 24 whereas the real factorial of 5 is 120!
Here is my fix for this bug.
for (int i= 1; i<=a+1; i++){
fact=fact*i;
adding the 1 to a (a+1) corrects the loop and gives an accurate output.
Have a great day.
Hassan/Canada

java scriptOrlan Villarino March 23, 2012 at 11:28 AM

How to run a program that include on how to block a number

Thks for posting.. useful example for begineers . i have one doubt why u given "Array out of bounds exception" if the input is double value?? does we can do with double Value??

## Thank you!!

Israel WondimuApril 16, 2011 at 5:52 PMContinue.....

## java

tasreef khanApril 19, 2011 at 3:41 PMwhat is BufferedReader and parseIntin in the program and what use these in the program.

## Factorial

shahinApril 21, 2011 at 3:53 PMI want to calculate factorial 256!. How can I do it & how can I display the result.

## java basic programs

BHARANI KUMARMay 26, 2011 at 1:02 PMplease forward me java basic programs which are mstly asked in interviews

## java and c++

sasikumar.pJune 28, 2011 at 9:41 PMis good

## factorial

charitoJuly 4, 2011 at 7:43 PMhow to factor at my age of 17..

## LOGIC AND PROGRAMMING

LAGOS,JACKIELOU C.July 29, 2011 at 8:52 AMTHE CODE HELP Me!!!

## logic and programming

LAGOS,JACKIELOU C.July 29, 2011 at 8:54 AMthank you!!!

## forloop

jhonAugust 22, 2011 at 11:08 AMi need help for calculating the factorial and triangular of a given number.

## worked like a charm

AshishAugust 22, 2011 at 12:26 PMthanks, it worked like a charm, very helpful. :)

## java

jim raeSeptember 29, 2011 at 5:49 AME Factorial Divisibility selected by Joe Horton The number (n!) is de ned to be the product of the n smallest positive integers. Thus 4! = 1 × 2 × 3 × 4. We want to know how many times you can divide 2 into (n!). Since 2 divides 12 once, and 2 divides 4 twice, but does not divide 3 at all, the number of times 2 divides (4!) is exactly three. Write a program to determine the number of times n! is divisible by 2. Input A sequence of positive integers all on one line, separated by spaces, and followed by 0. Output For each input integer n, excluding the last zero, print out a line: ”n factorial is divisible by exactly k 2’s.” Sample input 5 7 2 0 Output should be: 5 factorial is divisible by exactly 3 twos. 7 factorial is divisible by exactly 4 twos. 2 factorial is divisible by exactly 1 twos. (Ignor the bad grammar in the special case when the number of twos is 1.)

## hii

sriramOctober 22, 2011 at 11:35 AMpublic static String inString(String prompt) { inputFlush(); printPrompt(prompt); return inString(); } public static String inString() { int aChar; String s = ""; boolean finished = false; while (!finished) { try { aChar = System.in.read(); if (aChar < 0 || (char) aChar == '\n') finished = true; else if ((char) aChar != '\r') s = s + (char) aChar; // Enter into string } catch (java.io.IOException e) { System.out.println("Input error"); finished = true; } } return s; } public static int inInt(String prompt) { while (true) { inputFlush(); printPrompt(prompt); try { return Integer.valueOf(inString().trim()).intValue(); } catch (NumberFormatException e) { System.out.println("Invalid input. Not an integer"); } } } public static char inChar(String prompt) { int aChar = 0; inputFlush(); printPrompt(prompt); try { aChar = System.in.read(); } catch (java.io.IOException e) { System.out.println("Input error"); } inputFlush(); return (char) aChar; } public static double inDouble(String prompt) { while (true) { inputFlush(); printPrompt(prompt); try { return Double.valueOf(inString().trim()).doubleValue(); } catch (NumberFormatException e) { System.out .println("Invalid input. Not a floating point number"); } } } }

## regarding java

omkarOctober 27, 2011 at 1:23 PMnothing u are good enough to handle expectional handling

## java technology

yathartha sharmaNovember 28, 2011 at 8:59 PMthis is such a nice site and thanks for the answer

## send me same basics of java

anujaDecember 24, 2011 at 9:37 PMit was gud to refer

## :)

chachaJanuary 4, 2012 at 12:26 PMvery hard :P

## how to find sum of series of factorial values

deviJanuary 10, 2012 at 7:44 PMwrite a program to calculate & print the result of following series 1!+2!+3!+ . . . . . .+n! here n is any given number.

## java

sabirJanuary 15, 2013 at 12:10 PMwhat is recursion and input factorial in java programing

## Formula is off by 1

Hassan IrguiJanuary 26, 2012 at 11:28 PMI am just a beginner in Java programming. I looked up your int code for the factorial and compared it to an excel sheet output. I looks like your coding is missing the fact that the system will always start at zero even if your loop says i=1. With you code, the factorial of 5 for example is 24 whereas the real factorial of 5 is 120! Here is my fix for this bug. for (int i= 1; i<=a+1; i++){ fact=fact*i; adding the 1 to a (a+1) corrects the loop and gives an accurate output. Have a great day. Hassan/Canada

## java script

Orlan VillarinoMarch 23, 2012 at 11:28 AMHow to run a program that include on how to block a number

## Keywords

RiaMarch 23, 2012 at 9:15 PMWhat do the keywords "try" and "catch" mean in the code?

## I need one more clarrification

AnudeepJuly 24, 2012 at 5:14 PMThks for posting.. useful example for begineers . i have one doubt why u given "Array out of bounds exception" if the input is double value?? does we can do with double Value??

## comment

LavasAugust 21, 2012 at 4:14 PMHi, im a begginner, so how do i run it?

## java

marutiSeptember 12, 2012 at 11:06 AMjava is best subject

## this is correct one

selvaSeptember 16, 2012 at 2:16 PMthis is correct: fact=fact*a;

## java swing

ranjeet kumarOctober 11, 2012 at 2:19 AMsir, how to display 12 month calendar structure format in frame when i enter year,i only want its interface code plzs suggest me

## the dificulty of java

confuzedNovember 15, 2012 at 1:17 AMthis program gave me cancer

## hevlo everyone :)

paulineNovember 16, 2012 at 10:38 AMi0 want to know more about this java programming

## computer since

tahrir kilafahMay 2, 2013 at 12:48 PMhow i can make database

## program

Nithin josephAugust 16, 2013 at 8:24 PMfactorial system definde program

## Questions

Nithin josephAugust 16, 2013 at 8:28 PMfactoriyal program all?

Post your Comment