Check if a given number is factorial of any number
Given a number n, the task is to determine whether n can be a factorial of some number x
Examples:
Input: N = 24 Output: Yes Explanation: 4! = 24 Input: N = 25 Output: No
Approach:
Below is the implementation of the above approach:
C++
// C++ implementation for// the above approach#include <bits/stdc++.h>using namespace std;// Function to check if// the given number is a// factorial of any numberbool isFactorial(int n){ for (int i = 1;; i++) { if (n % i == 0) { n /= i; } else { break; } } if (n == 1) { return true; } else { return false; }}// Driver Codeint main(){ int n = 24; bool ans = isFactorial(n); if (ans == 1) { cout << "Yes\n"; } else { cout << "No\n"; } return 0;} |
Java
// Java implementation for the above approachclass GFG{ // Function to check if the given number // is a factorial of any number static boolean isFactorial(int n) { for (int i = 1;; i++) { if (n % i == 0) { n /= i; } else { break; } } if (n == 1) { return true; } else { return false; } } // Driver Code public static void main (String[] args) { int n = 24; boolean ans = isFactorial(n); if (ans == true) { System.out.println("Yes"); } else { System.out.println("No"); } }}// This code is contributed by AnkitRai01 |
Python3
# Python3 implementation of the approach# Function to check if# the given number is a# factorial of any numberdef isFactorial(n) : i = 1; while(True) : if (n % i == 0) : n //= i; else : break; i += 1; if (n == 1) : return True; else : return False;# Driver Codeif __name__ == "__main__" : n = 24; ans = isFactorial(n); if (ans == 1) : print("Yes"); else : print("No");# This code is contributed by kanugargng |
C#
// C# implementation for the above approachusing System; class GFG{ // Function to check if the given number // is a factorial of any number static Boolean isFactorial(int n) { for (int i = 1;; i++) { if (n % i == 0) { n /= i; } else { break; } } if (n == 1) { return true; } else { return false; } } // Driver Code public static void Main (String[] args) { int n = 24; Boolean ans = isFactorial(n); if (ans == true) { Console.WriteLine("Yes"); } else { Console.WriteLine("No"); } }}// This code is contributed by 29AjayKumar |
Javascript
<script>// Javascript implementation for// the above approach// Function to check if// the given number is a// factorial of any numberfunction isFactorial(n){ for (var i = 1;; i++) { if (n % i == 0) { n = parseInt(n/i); } else { break; } } if (n == 1) { return true; } else { return false; }}// Driver Codevar n = 24;var ans = isFactorial(n);if (ans == 1){ document.write("Yes");}else{ document.write("No");}// This code is contributed by noob2000.</script> |
Output:
Yes
Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.


