Click here to Skip to main content
15,899,754 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
#include<iostream>
using namespace std;
int fact(int n)
{
   int i=5;
   int c=n/i;
   while(c!=0)
   {
    i=i*i;
    c+=n/i;
    return c;
   }
   return 0; 
}
int main(){
    long t;
    unsigned int a[100001],x[100001];
    cin>>t;
    for(int i=0;i<t;i++)
    {
        cin>>a[i];
        int c=a[i];
        x[i]=fact(c);
    }
    for(int i=0;i<t;i++)
    {
    cout<<x[i]<<endl;
    }
}

The problem is to print the trailing zeroes in the factorial of a set of numbers and the numbers could be upto 100000. But I seem to get an incorrect answer for numbers with 4 digits or above.

What I have tried:

I tried simply putting the return c statement outside the loop but that makes the code go into infinite loop.
Posted
Updated 5-Jul-18 3:38am
v2
Comments
KarstenK 5-Jul-18 11:47am    
it is better to make more output, so the user sees the state of the calculations.

1 solution

Your factorial routine does not look correct, and the while loop will only ever do one iteration. Factorial N is 1*2*3...*N. You also need to get rid of those two huge arrays, they serve no purpose. The logic for your program should be:
Get number of tries
DO
    Get next number
    Calculate the factorial
    Display the number, and the factorial
    Set number of tries = (number of tries - 1)
    If number of tries is zero, break
    Repeat
END DO
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900