Answer to Question #324720 in Java | JSP | JSF for Holly

Question #324720

Factorials (JAVA)

by CodeChum Admin

For those of you who may not now, a factorial is a product of multiplying from 1 until the number. Now, you must make a program that will accept an integer and then print out its factorial using loops.

Are you up for this task?


Instructions:

  1. Input a positive integer.
  2. Using a for() loop, generate the factorial value of the integer and print out the result.
  3. Tip #1: Factorials work like this: Factorial of 5 = 1 * 2 * 3 * 4 * 5
  4. Tip #2: There's a special case in factorials. The factorial of 0 is 1.

Input

A line containing an integer.

5

Output

A line containing an integer.

120
1
Expert's answer
2022-04-08T01:49:50-0400
public static int getFactorial(int f) {
  int result = 1;
  for (int i = 1; i <= f; i++) {
     result = result * i;
  }
  return result;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS