-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSolution.java
More file actions
99 lines (68 loc) · 2.79 KB
/
Solution.java
File metadata and controls
99 lines (68 loc) · 2.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
//No longer do I call you servants, for the servant doesn't know what his lord does.
//But I have called you friends, for everything that I heard from my Father, I have made known to you. (John 15:15)
package com.javarush.task.task15.task1531;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigInteger;
/*
Факториал
*/
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
int input = Integer.parseInt(reader.readLine());
reader.close();
System.out.println(factorial(input));
}
public static String factorial(int n) {
if (n < 0) {
return "0";
} else if (n == 0) {
return "1";
} else {
return mathFactorial(BigInteger.valueOf(n)).toString();
}
}
public static BigInteger mathFactorial(BigInteger n) {
if (n.equals(BigInteger.ONE)) {
return n;
}
return n.multiply(mathFactorial(n.subtract(BigInteger.ONE)));
}
}
/*
Факториал
Написать метод, который вычисляет факториал — произведение всех чисел от 1 до введенного числа включая его.
Пример вычислений: 4! = factorial(4) = 1*2*3*4
Пример вывода: 24
1. Ввести с консоли число меньше либо равно 150.
2. Реализовать функцию factorial.
3. Если введенное число меньше 0, то вывести 0.
0! = 1
Требования:
1. Программа должна считывать данные с клавиатуры.
2. Программа должна выводить на экран факториал введенного числа.
3. Метод factorial должен возвращать строковое представление факториала числа переданного ему в качестве параметра.
4. Метод factorial должен принимать один параметр типа int.
package com.javarush.task.task15.task1531;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigDecimal;
*
Факториал
*
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
int input = Integer.parseInt(reader.readLine());
reader.close();
System.out.println(factorial(input));
}
public static String factorial(int n) {
//add your code here
return "";
}
}
*/