forked from jimmysingla0399/Java-FOP
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnonzerofact.java
More file actions
26 lines (22 loc) · 862 Bytes
/
nonzerofact.java
File metadata and controls
26 lines (22 loc) · 862 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
import java.util.*;
public class nonzerofact {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int n = s.nextInt();
System.out.print(lastNonZeroDigit(n));
}
// Method to find the last digit of n!
static int lastNonZeroDigit(int n) {
// initialise factorial value to 1
int factorial = 1;
// for loop to calculate the value of factorial
// you need not iterate the for loop for i=0 or 1 because if n=0 or 1 the factorial value is 1 and factorial
// is already initialized to 1
for (int i = 2; i <= n; i++)
factorial *= i;
// while loop to find the last non-zero digit of the factorial value
while (factorial % 10 == 0)
factorial /= 10;
return factorial % 10;
}
}