find number fact

C/C++ program to display the number if its factorial is given.if there is no such number print “-1”.

Input:
120
Output:
5

Input:
23
Output:
-1

@code_hacker please tell the range of the number N.


If the number is within the range of long long integer then start dividing the number from 2 onwards and check the remainder everytime. If it is 0, then proceed with other number else print -1.

Here is the program:

scanf("%lld",&n);
for(i=2;1;i++)
{
 if(n%i==0)
    n/=i;
  else if(n!=1)
  {
    printf("-1\n");
    break;
  }
  else
    break;
}
if(n==1)
printf("%d\n",i);

1 Like

according to your code the output is one more than the required number.

I think the last statement should be printf("%d",i-1);

the range of n is 1<=n<=65535

ok…thank you for pointing it out.

//