Last active
December 26, 2015 09:19
-
-
Save imryan/7128283 to your computer and use it in GitHub Desktop.
Calculates factorials of n. Possibly wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
Okay cool, thanks for the tip!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hey, if you don't mind some suggestions, I have one. You should go from
i = n
toi = 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.