Skip to content

Instantly share code, notes, and snippets.

@imryan
Last active December 26, 2015 09:19
Show Gist options
  • Save imryan/7128283 to your computer and use it in GitHub Desktop.
Save imryan/7128283 to your computer and use it in GitHub Desktop.
Calculates factorials of n. Possibly wrong.
import java.util.*;
public class Factorial {
public static void main(String[] args)
{
// Declare variables
Scanner sc = new Scanner(System.in);
int n = 0, total = 1;
// Get input for n
n = sc.nextInt();
for (int i = n; i >= 1; i--)
{
total *= i;
}
System.out.println(n + "! = " + total);
}
}
@LBijaminas
Copy link

Hey, if you don't mind some suggestions, I have one. You should go from i = n to i = 1, i--. The only reason for that is that it reflects more what the factorial is, than counting upwards.
Anyway, the answer is still correct.

@imryan
Copy link
Author

imryan commented Oct 24, 2013

Okay cool, thanks for the tip!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment