find if a given number is a factorial

Mar 19, 2013 at 6:19am
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.
Mar 19, 2013 at 7:02am
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 Mar 19, 2013 at 7:03am
Mar 19, 2013 at 12:58pm
I don't understand. Can you explain with an appropriate program and comments so that it would be easier for me to understand?

Thanks
Mar 19, 2013 at 1:13pm
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
}
Mar 19, 2013 at 3:48pm
Thank you so much. It makes sense now.
Topic archived. No new replies allowed.