Skip to content

Commit 256718b

Browse files
authored
Adding Factorial Program
Adding a basic program to find the factorial of a given number.
1 parent f202f38 commit 256718b

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

Basic-Programs/factorial.java

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import java.util.Scanner;
2+
3+
class Factorial
4+
{
5+
public static void main(String args[])
6+
{
7+
int n, c, f = 1;
8+
9+
System.out.println("Enter an integer to calculate its factorial");
10+
Scanner in = new Scanner(System.in);
11+
12+
n = in.nextInt();
13+
14+
if (n < 0)
15+
System.out.println("Number should be non-negative.");
16+
else
17+
{
18+
for (c = 1; c <= n; c++)
19+
f = f*c;
20+
21+
System.out.println("Factorial of "+n+" is = "+f);
22+
}
23+
}
24+
}

0 commit comments

Comments
 (0)