• Sunday, 13 March 2016

    Write a program that will calculate the number of trailing zeros in a factorial of a given number

    First off, let’s define a factorial for those of you who don’t remember what a factorial of a number is. A factorial of any non-negative integer number “x” is the product of all of the positive integers less than and equal to “x”. This means that the factorial of 4 is 4*3*2*1 which is 24, and the factorial of 6 is 6*5*4*3*2*1 which is 720.

    What does the number of trailing zeros mean exactly?


    If we want to find the number of trailing zeros this means that we want to calculate how many zeros the factorial of a number ends with – the number of terminating zerro.

    #include<stdio.h> #include<math.h> int main() { int n,count=0,i=1; printf("Md.Mostafa Kamal\n"); printf("\n\n\n enter n: "); scanf("%d",&n); do{ count=count+n/(5*i); i*=5; } while(i<=n); printf("No of zeros=%d",count); return 0; }
    output result:



    { 1 comments... read them below or add one }

  • - Copyright © Computer Programming LIVE - Powered by Nishi IT Ltd. - Designed by mostafa -