Home > Backend Development > C++ > body text

Given a factorial, write a C program to find the trailing zeros

WBOY
Release: 2023-09-18 08:25:02
forward
522 people have browsed it

Given a factorial, write a C program to find the trailing zeros

To find the trailing zeros in a given factorial, let us consider the following three examples:

Example 1

Input - 4

Output - 0

Explanation - 4! = 24, no trailing zero.

Factorial 4! = 4 x 3 x 2 x 1 = 24. There is no number 4 in the place of the trailing zero.

Example 2

Input - 6

Output - 1

Explanation - 6! = 720 , has a trailing zero.

Factorial 6! = 6 x 5 x 4 x 3 x 2 x 1 = 720, there is a trailing zero because there is a number 0 in the place of the trailing zero.

Example 3

The input is as follows-

n = 4
n = 5
Copy after login

The output is as follows-

4! The number of trailing zeros is 0## The number of trailing zeros for

#5! is 1

Example

The following is a C program for

finding the trailing zeros of a given factorial−

Online Demonstration

#include <stdio.h>
static int trailing_Zeroes(int n){
   int number = 0;
   while (n > 0) {
      number += n / 5;
      n /= 5;
   }
   return number;
}
int main(void){
   int n;
   printf("enter integer1:");
   scanf("%d",&n);
   printf("</p><p> no: of trailing zeroe&#39;s of factorial %d is %d</p><p></p><p> ", n, trailing_Zeroes(n));
   printf("enter integer2:");
   scanf("%d",&n);
   printf("</p><p> no: of trailing zeroe&#39;s of factorial %d is %d ", n, trailing_Zeroes(n));
   return 0;
}
Copy after login

Output

When the above program is executed, it produces the following results−

enter integer1:5
no: of trailing zeroe&#39;s of factorial 5 is 1
enter integer2:6
no: of trailing zeroe&#39;s of factorial 6 is 1
Copy after login

The above is the detailed content of Given a factorial, write a C program to find the trailing zeros. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template