FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
Java/Factorial.java at master · rcanto/Java · GitHub
rcanto
/
Java
Public
forked from
TheAlgorithms/Java
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
Java
/
Factorial.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
35 lines (30 loc) · 843 Bytes
Breadcrumbs
Java
/
Factorial.java
Copy path
File metadata and controls
35 lines (30 loc) · 843 Bytes
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
import
java
.
util
.
Scanner
;
public
class
Factorial
{
public
static
void
main
(
String
[]
args
){
Scanner
input
=
new
Scanner
(
System
.
in
);
//Prompt user to enter integer
System
.
out
.
print
(
"Enter a non-negative integer: "
);
//Proceed with factorial calculation only if inputted number is not negative
if
(
input
.
hasNextInt
()){
int
number
=
input
.
nextInt
();
if
(
number
<
0
){
System
.
out
.
print
(
"Cannot execute. Please enter a non-negative integer: "
);
number
=
input
.
nextInt
();
}
else
{
//Output of factorial for any non-negative number
System
.
out
.
println
(
"The factorial of "
+
number
+
" will yield: "
+
factorial
(
number
));
}
}
}
//Factorial method
public
static
long
factorial
(
int
n
){
// DEMASIADAS LLAVES
if
(
n
==
0
){
return
1
;
}
else
if
(
n
==
1
){
return
1
;
}
else
{
return
n
*
factorial
(
n
-
1
);
}
}
}
Back
|
FazBrowse Home
|
New Git URL