search
HomeBackend DevelopmentC++Given a factorial, write a C program to find the trailing zeros

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. If there is any infringement, please contact admin@php.cn delete
c语言怎么计算n的阶乘c语言怎么计算n的阶乘Jan 04, 2023 pm 03:18 PM

c语言计算n的阶乘的方法:1、通过for循环计算阶乘,代码如“for (i = 1; i <= n; i++){fact *= i;}”;2、通过while循环计算阶乘,代码如“while (i <= n){fact *= i;i++;}”;3、通过递归方式计算阶乘,代码如“ int Fact(int n){int res = n;if (n > 1)res...”。

C程序中的阶乘程序C程序中的阶乘程序Sep 09, 2023 am 11:17 AM

Givenwiththenumbernthetaskistocalculatethefactorialofanumber.Factorialofanumberiscalculatedbymultiplyingthenumberwithitssmallestorequalintegervalues.Factorialiscalculatedas&minus;0!=11!=12!=2X1=23!=3X2X1=64!=4X3X2X1=245!=5X4X3X2X1=120...N!=n*(n-1

PHP程序计算一个数的阶乘中末尾零的个数PHP程序计算一个数的阶乘中末尾零的个数Aug 26, 2023 pm 05:17 PM

阶乘是什么?Thefactorialofanon-negativeinteger,denotedbythesymbol"!",istheproductofallpositiveintegerslessthanorequaltothatnumber.Inotherwords,thefactorialofanumberisobtainedbymultiplyingthatnumberbyallthepositiveintegersbelowit.Forexample,thefac

计算一个数的阶乘中末尾零的个数的C/C++编程?计算一个数的阶乘中末尾零的个数的C/C++编程?Sep 20, 2023 pm 10:05 PM

计算阶乘数中末尾零的个数是通过计算该数的因子中2和5的个数来完成的。因为2*5等于10,而10是阶乘数中的末尾零。示例7的阶乘=5040,末尾0的个数为1。根据我们的逻辑,7!=2*3*4*5*6*7,它有3个2和1个5,所以末尾0的个数为1。#include<iostream>usingnamespacestd;intmain(){&nbsp;&nbsp;intn=45;&nbsp;&nbsp;intcount=0;&nbsp;&nb

如何使用Go语言递归函数实现阶乘?如何使用Go语言递归函数实现阶乘?Jul 31, 2023 pm 08:31 PM

如何使用Go语言递归函数实现阶乘?阶乘是数学中常见的一种计算方式,它将一个非负整数n乘以比它小的所有正整数,直到1。例如,5的阶乘可以表示为5!,计算方式为54321=120。在计算机编程中,我们经常使用递归函数来实现阶乘的计算。首先,我们需要了解递归函数的概念。递归函数是指在函数的定义中调用函数本身的过程。在解决问题时,递归函数会不断地

python计算数的阶乘的方法python计算数的阶乘的方法Nov 13, 2023 am 11:30 AM

方法:1、使用循环;2、使用递归;3、使用math模块;4、使用reduce函数。

php怎么实现1到10的阶乘php怎么实现1到10的阶乘Jan 18, 2023 am 11:02 AM

php实现1到10的阶乘的方法:1、创建一个PHP示例文件;2、赋值变量result为1;3、通过“for($i=2;$i<=$n;$i++)”循环语句将i从2开始循环并逐次加1;4、定义“$result*=$i;”公式;5、通过echo输出10的阶乘结果即可。

如何使用PHP去除字符串末尾两个字符如何使用PHP去除字符串末尾两个字符Mar 24, 2024 pm 05:54 PM

如何使用PHP去除字符串末尾两个字符?在PHP中,可以使用内置函数substr()来实现去除字符串末尾两个字符的操作。substr()函数用于返回字符串的一部分,在本例中,我们可以利用它来获取原始字符串的除了末尾两个字符之外的部分。接下来,我将展示具体的代码示例来说明如何实现这一操作。首先,我们需要定义一个包含原始字符串的变量,然后使用sub

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment