﻿ Java exercises: Compute the number of trailing zeros in a factorial - w3resource # Java Exercises: Compute the number of trailing zeros in a factorial

## Java Basic: Exercise-112 with Solution

Write a Java program to compute the number of trailing zeros in a factorial.

Example
7! = 5040, therefore the output should be 1.

Pictorial Presentation: Sample Solution:

Java Code:

``````import java.util.Scanner;
public class Example112 {
public static void main(String[] arg)
{

Scanner in = new Scanner(System.in);
System.out.print("Input a number: ");
int n = in.nextInt();
int n1 = n;
long ctr = 0;
while (n != 0)
{
ctr += n / 5;
n /= 5;
}
System.out.printf("Number of trailing zeros of the factorial %d is %d ",n1,ctr);
System.out.printf("\n");
}
}
```
```

Sample Output:

```Input a number : 5040
Number of trailing zeros of the factorial 5040 is 1258
```

Flowchart: Java Code Editor:

What is the difficulty level of this exercise?

﻿

## Java: Tips of the Day

initial:

Returns all the elements of an array except the last one. Use Arrays.copyOfRange() to return all except the last one

```public static <T> T[] initial(T[] elements) {
return Arrays.copyOfRange(elements, 0, elements.length - 1);
}
```

Ref: https://bit.ly/3zEKw9u