IT/Java
Java (for문_팩토리얼)
hjshims
2021. 4. 16. 11:26
for문을 사용한 팩토리얼 예제
package p0416;
import java.util.Scanner;
public class Factorial {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scan = new Scanner(System.in);
int i, fact = 1, result = 0;
System.out.print("정수를 입력하세요: ");
result = scan.nextInt();
System.out.print(result + "! = ");
for (i = result; i > 0; i--) {
fact = fact * i;
if (i >= 2) {
System.out.print(i + "*");
} else {
System.out.print(i);
}
}
System.out.print("\n" + result + "! = " + fact);
}
}