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

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

Sep 18, 2023 am 08:25 AM
factorial end zero

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!

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

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1666
14
PHP Tutorial
1273
29
C# Tutorial
1253
24
How to calculate the factorial of n in C language How to calculate the factorial of n in C language Jan 04, 2023 pm 03:18 PM

How to calculate the factorial of n in C language: 1. Calculate the factorial through a for loop, with code such as "for (i = 1; i <= n; i++){fact *= i;}"; 2. Calculate the factorial through a while loop, Code such as "while (i <= n){fact *= i;i++;}"; 3. Calculate factorial recursively, code such as "int Fact(int n){int res = n;if (n > 1) res...".

Factorial program in C program Factorial program in C program Sep 09, 2023 am 11:17 AM

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

C/C++ programming to calculate the number of trailing zeros in the factorial of a number? C/C++ programming to calculate the number of trailing zeros in the factorial of a number? Sep 20, 2023 pm 10:05 PM

Calculating the number of trailing zeros in a factorial number is done by counting the number of 2's and 5's in the factors of the number. Because 2*5 is equal to 10, and 10 is the last zero in the factorial number. The factorial of Example 7 = 5040, and the number of 0s at the end is 1. According to our logic, 7!=2*3*4*5*6*7, which has 3 2s and 1 5, so the number of 0s at the end is 1. #include<iostream>usingnamespacestd;intmain(){ intn=45; intcount=0; &nb

How to implement factorial using recursive functions in Go language? How to implement factorial using recursive functions in Go language? Jul 31, 2023 pm 08:31 PM

How to implement factorial using recursive functions in Go language? Factorial is a common calculation in mathematics that multiplies a non-negative integer n by all positive integers smaller than it, up to 1. For example, the factorial of 5 can be expressed as 5!, calculated as 54321=120. In computer programming, we often use recursive functions to implement factorial calculations. First, we need to understand the concept of recursive functions. A recursive function refers to the process of calling the function itself within the definition of the function. When solving a problem, a recursive function will continually

How to implement factorial from 1 to 10 in php How to implement factorial from 1 to 10 in php Jan 18, 2023 am 11:02 AM

How to implement factorial from 1 to 10 in PHP: 1. Create a PHP sample file; 2. Assign the variable result to 1; 3. Use the "for($i=2;$i<=$n;$i++)" loop statement Loop i from 2 and add 1 successively; 4. Define the "$result*=$i;" formula; 5. Output the factorial result of 10 through echo.

How to remove the last two characters from a string using PHP How to remove the last two characters from a string using PHP Mar 24, 2024 pm 05:54 PM

How to remove the last two characters of a string using PHP? In PHP, you can use the built-in function substr() to remove the last two characters of a string. The substr() function is used to return a part of a string. In this case, we can use it to get the part of the original string except the last two characters. Next, I'll show you a specific code example of how to do this. First we need to define a variable containing the original string and then use sub

How to calculate the factorial of a number in Python How to calculate the factorial of a number in Python Nov 13, 2023 am 11:30 AM

Method: 1. Use loop; 2. Use recursion; 3. Use math module; 4. Use reduce function.

PHP program to calculate the number of trailing zeros in the factorial of a number PHP program to calculate the number of trailing zeros in the factorial of a number Aug 26, 2023 pm 05:17 PM

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

See all articles