콘솔창에 입력된 숫자의 Factorial 값을 구하시오.
페이지 정보
작성자 최고관리자 작성일 70-01-01 09:00 조회 307 댓글 0본문
[code=php]
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Factorial을 구할 수는 : ");
int number = sc.nextInt();
int factorialsum = factorial(number);
System.out.println(number + "! = " + factorialsum);
}
private static int factorial(int number) {
int sum = 1;
for (int i = 2; i<= number; i++) {
sum = sum * i; //sum *= i;
}
return sum;
}
}
[/code]
재귀함수를 사용하여 구하기...
[code=php]
import java.util.Scanner;
public class Main {
private static Scanner sc;
public static void main(String[] args) {
sc = new Scanner(System.in);
System.out.print("Factorial을 구할 수는 : ");
int number = sc.nextInt();
int factorialsum = factorial(number);
System.out.println(number + "! = " + factorialsum);
}
private static int factorial(int number) {
if (number ==1) {
return 1;
} else {
return number * factorial(number -1);
}
}
}
[/code]
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Factorial을 구할 수는 : ");
int number = sc.nextInt();
int factorialsum = factorial(number);
System.out.println(number + "! = " + factorialsum);
}
private static int factorial(int number) {
int sum = 1;
for (int i = 2; i<= number; i++) {
sum = sum * i; //sum *= i;
}
return sum;
}
}
[/code]
재귀함수를 사용하여 구하기...
[code=php]
import java.util.Scanner;
public class Main {
private static Scanner sc;
public static void main(String[] args) {
sc = new Scanner(System.in);
System.out.print("Factorial을 구할 수는 : ");
int number = sc.nextInt();
int factorialsum = factorial(number);
System.out.println(number + "! = " + factorialsum);
}
private static int factorial(int number) {
if (number ==1) {
return 1;
} else {
return number * factorial(number -1);
}
}
}
[/code]
댓글목록 0
등록된 댓글이 없습니다.