Determine if the Number is a Factorial Value

The program must accept an integer N as the input. The task is to determine if N can be expressed as a factorial of an integer. If possible, the program should print “YES” and the integer value whose factorial is N. Otherwise, print “NO”.

Boundary Condition(s):
1 <= N <= 10^18

Input Format:
The first line contains the integer N.

Output Format:
If the number can be expressed as a factorial, the first line contains “YES” followed by the integer value. Otherwise, print “NO”.

Example Input/Output 1:
Input:
24

Output:
YES 4

Explanation:
24 is the factorial of 4 (i.e., 4! = 4 x 3 x 2 x 1 = 24). Hence the output is YES 4.

Example Input/Output 2:
Input:
23

Output:
NO

Explanation:
23 cannot be expressed as the factorial of any integer. Hence the output is NO.

N = int(input().strip())
i = 1
factorial = 1
while factorial < N:
    i += 1
    factorial *= i
if factorial == N:
    print("YES", i)
else:
    print("NO")
#include<stdio.h>

int main() {
    long long int N;
    scanf("%lld", &N);
    long long int factorial = 1;
    int i = 1;
    while(factorial < N) {
        i++;
        factorial *= i;
    }
    if(factorial == N) {
        printf("YES %d", i);
    } else {
        printf("NO");
    }
    return 0;
}
#include <iostream>
using namespace std;

int main() {
    long long int N;
    cin >> N;
    long long int factorial = 1;
    int i = 1;
    while(factorial < N) {
        i++;
        factorial *= i;
    }
    if(factorial == N) {
        cout << "YES " << i;
    } else {
        cout << "NO";
    }
    return 0;
}
import java.util.Scanner;

public class IsFactorialValue {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        long N = sc.nextLong();
        long factorial = 1;
        int i = 1;
        while(factorial < N) {
            i++;
            factorial *= i;
        }
        if(factorial == N) {
            System.out.println("YES " + i);
        } else {
            System.out.println("NO");
        }
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *

More posts. You may also be interested in.