find if a given number is a factorial

I need help to find if a given number is a factorial or not. Example : if 120 is given, it is a factorial as 120 = 5! Whereas 121 is not. Any help will be appreciated.
divide by natural numbers, incrementing them by 1 each time in a while loop.
if there's a non zero remainder, its not a factorial.
if finally the divisisor becomes one, it is a factorial.
as in
int i=0;
while(...)
n=n/i;
i++

Last edited on
I don't understand. Can you explain with an appropriate program and comments so that it would be easier for me to understand?

Thanks
1
2
3
4
5
6
7
8
int findFact(int test){
    for(int i=1, int sum=1; sum <= test; i++){
        if(sum == test)
            return i; //Factorial of i
        sum *= i; //Increment factorial number
    }
    return 0; //No factorial found
}
Thank you so much. It makes sense now.
Topic archived. No new replies allowed.