Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit e89bb04

Browse files
🍵
1 parent 842e464 commit e89bb04

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

‎challenge18.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import java.util.Scanner;
2+
3+
// Write a function that calculates the factorial of a given number.🚀
4+
public class challenge18 {
5+
public static void main(String[] args) {
6+
Scanner sc = new Scanner(System.in);
7+
System.out.println("Welcome to factorial");
8+
System.out.print("Enter the number: ");
9+
int num = sc.nextInt();
10+
long fact = factorial(num);
11+
System.out.println("Factorial is: " + fact);
12+
}
13+
14+
public static long factorial(int num){
15+
if (num < 2){
16+
return 1;
17+
}
18+
long fact = 1;
19+
int i = 2;
20+
while(i <= num){
21+
fact *= i;
22+
i++;
23+
}
24+
return fact;
25+
}
26+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /