-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFactorial.java
More file actions
34 lines (28 loc) · 799 Bytes
/
Factorial.java
File metadata and controls
34 lines (28 loc) · 799 Bytes
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
/*
* Program to find the factorial of a number without recursion: Write a Java program to calculate
* the factorial of a given number without using recursion.
*/
import java.util.Scanner;
public class Factorial {
public static int find_factorial(int n)
{
int factorial = 1;
if((n==0) || (n==1))
{
factorial = 1;
}
for(int i=2; i<n+1; i++)
{
factorial = factorial * i;
}
return factorial;
}
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number to find it's factorial: ");
int n = sc.nextInt();
sc.close();
System.out.println("Factorial is: " + find_factorial(n));
}
}