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 Wondimu April 16, 2011 at 5:52 PM
Continue.....
javatasreef khan April 19, 2011 at 3:41 PM
what is BufferedReader and parseIntin in the program and what use these in the program.
Factorialshahin April 21, 2011 at 3:53 PM
I want to calculate factorial 256!. How can I do it & how can I display the result.
java basic programsBHARANI KUMAR May 26, 2011 at 1:02 PM
please forward me java basic programs which are mstly asked in interviews
java and c++sasikumar.p June 28, 2011 at 9:41 PM
is good
factorialcharito July 4, 2011 at 7:43 PM
how to factor at my age of 17..
LOGIC AND PROGRAMMINGLAGOS,JACKIELOU C. July 29, 2011 at 8:52 AM
THE CODE HELP Me!!!
logic and programmingLAGOS,JACKIELOU C. July 29, 2011 at 8:54 AM
thank you!!!
javajim rae September 29, 2011 at 5:49 AM
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.)
hiisriram October 22, 2011 at 11:35 AM
public 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 javaomkar October 27, 2011 at 1:23 PM
nothing u are good enough to handle expectional handling
java technologyyathartha sharma November 28, 2011 at 8:59 PM
this is such a nice site and thanks for the answer
send me same basics of java anuja December 24, 2011 at 9:37 PM
it was gud to refer
:)chacha January 4, 2012 at 12:26 PM
very hard :P
how to find sum of series of factorial valuesdevi January 10, 2012 at 7:44 PM
write a program to calculate & print the result of following series 1!+2!+3!+ . . . . . .+n! here n is any given number.
java sabir January 15, 2013 at 12:10 PM
what is recursion and input factorial in java programing
Formula is off by 1Hassan Irgui January 26, 2012 at 11:28 PM
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
Keywords Ria March 23, 2012 at 9:15 PM
What do the keywords "try" and "catch" mean in the code?
I need one more clarrificationAnudeep July 24, 2012 at 5:14 PM
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??
commentLavas August 21, 2012 at 4:14 PM
Hi, im a begginner, so how do i run it?
javamaruti September 12, 2012 at 11:06 AM
java is best subject
this is correct oneselva September 16, 2012 at 2:16 PM
this is correct: fact=fact*a;
java swingranjeet kumar October 11, 2012 at 2:19 AM
sir, 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 javaconfuzed November 15, 2012 at 1:17 AM
this program gave me cancer
hevlo everyone :)pauline November 16, 2012 at 10:38 AM
i0 want to know more about this java programming
computer sincetahrir kilafah May 2, 2013 at 12:48 PM
how i can make database
Post your Comment