Home  >  Article  >  Backend Development  >  Given a factorial, write a C program to find the trailing zeros

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

WBOY
WBOYforward
2023-09-18 08:25:02487browse

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

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;
}

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

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!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete