Check if a given number is factorial of other

Here given code implementation process.

// C program
// Check if a given number is factorial of other 
#include <stdio.h>

//Check whether given number is factorial of any number
void is_factorial_of(int num)
{
	int result = 0;
  
	if (num == 1 || num == 0)
	{
		result = 1;
	}
	else if (num > 1)
	{
		//Define loop controlling variable
		int temp = num;
		int i = 1;
		// Execute loop, until number is divisible by [i]
		while (temp % i == 0)
		{
			temp = temp / i;
			// Get next number
			i++;
		}
		if (temp == 1)
		{
			//When remainder of (i-1) is one 
			result = i - 1;
		}
	}
	if (result != 0)
	{
		printf("\n [%d] is factorial of %d", num, result);
	}
	else
	{
		printf("\n [%d] is not factorial of any number", num);
	}
}
int main()
{
	is_factorial_of(24);
	is_factorial_of(2);
	is_factorial_of(34);
	is_factorial_of(720);
	is_factorial_of(120);
	is_factorial_of(0);
	return 0;
}

Output

 [24] is factorial of 4
 [2] is factorial of 2
 [34] is not factorial of any number
 [720] is factorial of 6
 [120] is factorial of 5
 [0] is factorial of 1
// Java program 
// Check if a given number is factorial of other 
class FactorialNo
{
	//Check whether given number is factorial of any number
	public void is_factorial_of(int num)
	{
		int result = 0;
		if (num == 1 || num == 0)
		{
			result = 1;
		}
		else if (num > 1)
		{
			//Define loop controlling variable
			int temp = num;
			int i = 1;
			// Execute loop, until number is divisible by [i]
			while (temp % i == 0)
			{
				temp = temp / i;
				// Get next number
				i++;
			}
			if (temp == 1)
			{
				//When remainder of (i-1) is one 
				result = i - 1;
			}
		}
		if (result != 0)
		{
			System.out.print("\n [" + num + "] is factorial of " + result);
		}
		else
		{
			System.out.print("\n [" + num + "] is not factorial of any number");
		}
	}
	public static void main(String[] args)
	{
		FactorialNo obj = new FactorialNo();
		// Test Case
		obj.is_factorial_of(24);
		obj.is_factorial_of(2);
		obj.is_factorial_of(34);
		obj.is_factorial_of(720);
		obj.is_factorial_of(120);
		obj.is_factorial_of(0);
	}
}

Output

 [24] is factorial of 4
 [2] is factorial of 2
 [34] is not factorial of any number
 [720] is factorial of 6
 [120] is factorial of 5
 [0] is factorial of 1
//Include header file
#include <iostream>
using namespace std;

// C++ program 
// Check if a given number is factorial of other 

class FactorialNo
{
	public:
		//Check whether given number is factorial of any number
		void is_factorial_of(int num)
		{
			int result = 0;
			if (num == 1 || num == 0)
			{
				result = 1;
			}
			else if (num > 1)
			{
				//Define loop controlling variable
				int temp = num;
				int i = 1;
				// Execute loop, until number is divisible by [i]
				while (temp % i == 0)
				{
					temp = temp / i;
					// Get next number
					i++;
				}
				if (temp == 1)
				{
					//When remainder of (i-1) is one 
					result = i - 1;
				}
			}
			if (result != 0)
			{
				cout << "\n [" << num << "] is factorial of " << result;
			}
			else
			{
				cout << "\n [" << num << "] is not factorial of any number";
			}
		}
};
int main()
{
	FactorialNo obj = FactorialNo();
	// Test Case
	obj.is_factorial_of(24);
	obj.is_factorial_of(2);
	obj.is_factorial_of(34);
	obj.is_factorial_of(720);
	obj.is_factorial_of(120);
	obj.is_factorial_of(0);
	return 0;
}

Output

 [24] is factorial of 4
 [2] is factorial of 2
 [34] is not factorial of any number
 [720] is factorial of 6
 [120] is factorial of 5
 [0] is factorial of 1
//Include namespace system
using System;

// C# program 
// Check if a given number is factorial of other 

class FactorialNo
{
	//Check whether given number is factorial of any number
	public void is_factorial_of(int num)
	{
		int result = 0;
		if (num == 1 || num == 0)
		{
			result = 1;
		}
		else if (num > 1)
		{
			//Define loop controlling variable
			int temp = num;
			int i = 1;
			// Execute loop, until number is divisible by [i]
			while (temp % i == 0)
			{
				temp = temp / i;
				// Get next number
				i++;
			}
			if (temp == 1)
			{
				//When remainder of (i-1) is one 
				result = i - 1;
			}
		}
		if (result != 0)
		{
			Console.Write("\n [" + num + "] is factorial of " + result);
		}
		else
		{
			Console.Write("\n [" + num + "] is not factorial of any number");
		}
	}
	public static void Main(String[] args)
	{
		FactorialNo obj = new FactorialNo();
		// Test Case
		obj.is_factorial_of(24);
		obj.is_factorial_of(2);
		obj.is_factorial_of(34);
		obj.is_factorial_of(720);
		obj.is_factorial_of(120);
		obj.is_factorial_of(0);
	}
}

Output

 [24] is factorial of 4
 [2] is factorial of 2
 [34] is not factorial of any number
 [720] is factorial of 6
 [120] is factorial of 5
 [0] is factorial of 1
<?php
// Php program 
// Check if a given number is factorial of other 

class FactorialNo
{
	//Check whether given number is factorial of any number
	public	function is_factorial_of($num)
	{
		$result = 0;
		if ($num == 1 || $num == 0)
		{
			$result = 1;
		}
		else if ($num > 1)
		{
			//Define loop controlling variable
			$temp = $num;
			$i = 1;
			// Execute loop, until number is divisible by [i]
			while ($temp % $i == 0)
			{
				$temp = intval($temp / $i);
				// Get next number
				$i++;
			}
			if ($temp == 1)
			{
				//When remainder of (i-1) is one 
				$result = $i - 1;
			}
		}
		if ($result != 0)
		{
			echo "\n [". $num ."] is factorial of ". $result;
		}
		else
		{
			echo "\n [". $num ."] is not factorial of any number";
		}
	}
}

function main()
{
	$obj = new FactorialNo();
	// Test Case
	$obj->is_factorial_of(24);
	$obj->is_factorial_of(2);
	$obj->is_factorial_of(34);
	$obj->is_factorial_of(720);
	$obj->is_factorial_of(120);
	$obj->is_factorial_of(0);
}
main();

Output

 [24] is factorial of 4
 [2] is factorial of 2
 [34] is not factorial of any number
 [720] is factorial of 6
 [120] is factorial of 5
 [0] is factorial of 1
// Node Js program 
// Check if a given number is factorial of other 
class FactorialNo
{
	//Check whether given number is factorial of any number
	is_factorial_of(num)
	{
		var result = 0;
		if (num == 1 || num == 0)
		{
			result = 1;
		}
		else if (num > 1)
		{
			//Define loop controlling variable
			var temp = num;
			var i = 1;
			// Execute loop, until number is divisible by [i]
			while (temp % i == 0)
			{
				temp = parseInt(temp / i);
				// Get next number
				i++;
			}
			if (temp == 1)
			{
				//When remainder of (i-1) is one 
				result = i - 1;
			}
		}
		if (result != 0)
		{
			process.stdout.write("\n [" + num + "] is factorial of " + result);
		}
		else
		{
			process.stdout.write("\n [" + num + "] is not factorial of any number");
		}
	}
}

function main()
{
	var obj = new FactorialNo();
	// Test Case
	obj.is_factorial_of(24);
	obj.is_factorial_of(2);
	obj.is_factorial_of(34);
	obj.is_factorial_of(720);
	obj.is_factorial_of(120);
	obj.is_factorial_of(0);
}
main();

Output

 [24] is factorial of 4
 [2] is factorial of 2
 [34] is not factorial of any number
 [720] is factorial of 6
 [120] is factorial of 5
 [0] is factorial of 1
#  Python 3 program 
#  Check if a given number is factorial of other 
class FactorialNo :
	# Check whether given number is factorial of any number
	def is_factorial_of(self, num) :
		result = 0
		if (num == 1 or num == 0) :
			result = 1
		
		elif(num > 1) :
			# Define loop controlling variable
			temp = num
			i = 1
			#  Execute loop, until number is divisible by [i]
			while (temp % i == 0) :
				temp = int(temp / i)
				#  Get next number
				i += 1
			
			if (temp == 1) :
				# When remainder of (i-1) is one 
				result = i - 1
			
		
		if (result != 0) :
			print("\n [", num ,"] is factorial of ", result, end = "")
		else :
			print("\n [", num ,"] is not factorial of any number", end = "")
		
	

def main() :
	obj = FactorialNo()
	#  Test Case
	obj.is_factorial_of(24)
	obj.is_factorial_of(2)
	obj.is_factorial_of(34)
	obj.is_factorial_of(720)
	obj.is_factorial_of(120)
	obj.is_factorial_of(0)

if __name__ == "__main__": main()

Output

 [ 24 ] is factorial of  4
 [ 2 ] is factorial of  2
 [ 34 ] is not factorial of any number
 [ 720 ] is factorial of  6
 [ 120 ] is factorial of  5
 [ 0 ] is factorial of  1
#  Ruby program 
#  Check if a given number is factorial of other 
class FactorialNo 
	# Check whether given number is factorial of any number
	def is_factorial_of(num) 
		result = 0
		if (num == 1 || num == 0) 
			result = 1
		elsif(num > 1) 
			# Define loop controlling variable
			temp = num
			i = 1
			#  Execute loop, until number is divisible by [i]
			while (temp % i == 0) 
				temp = temp / i
				#  Get next number
				i += 1
			end

			if (temp == 1) 
				# When remainder of (i-1) is one 
				result = i - 1
			end

		end

		if (result != 0) 
			print("\n [", num ,"] is factorial of ", result)
		else 
			print("\n [", num ,"] is not factorial of any number")
		end

	end

end

def main() 
	obj = FactorialNo.new()
	#  Test Case
	obj.is_factorial_of(24)
	obj.is_factorial_of(2)
	obj.is_factorial_of(34)
	obj.is_factorial_of(720)
	obj.is_factorial_of(120)
	obj.is_factorial_of(0)
end

main()

Output

 [24] is factorial of 4
 [2] is factorial of 2
 [34] is not factorial of any number
 [720] is factorial of 6
 [120] is factorial of 5
 [0] is factorial of 1
// Scala program 
// Check if a given number is factorial of other 
class FactorialNo
{
	//Check whether given number is factorial of any number
	def is_factorial_of(num: Int): Unit = {
		var result: Int = 0;
		if (num == 1 || num == 0)
		{
			result = 1;
		}
		else if (num > 1)
		{
			//Define loop controlling variable
			var temp: Int = num;
			var i: Int = 1;
			// Execute loop, until number is divisible by [i]
			while (temp % i == 0)
			{
				temp = (temp / i).toInt;
				// Get next number
				i += 1;
			}
			if (temp == 1)
			{
				//When remainder of (i-1) is one 
				result = i - 1;
			}
		}
		if (result != 0)
		{
			print("\n [" + num + "] is factorial of " + result);
		}
		else
		{
			print("\n [" + num + "] is not factorial of any number");
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var obj: FactorialNo = new FactorialNo();
		// Test Case
		obj.is_factorial_of(24);
		obj.is_factorial_of(2);
		obj.is_factorial_of(34);
		obj.is_factorial_of(720);
		obj.is_factorial_of(120);
		obj.is_factorial_of(0);
	}
}

Output

 [24] is factorial of 4
 [2] is factorial of 2
 [34] is not factorial of any number
 [720] is factorial of 6
 [120] is factorial of 5
 [0] is factorial of 1
// Swift 4 program 
// Check if a given number is factorial of other 
class FactorialNo
{
	//Check whether given number is factorial of any number
	func is_factorial_of(_ num: Int)
	{
		var result: Int = 0;
		if (num == 1 || num == 0)
		{
			result = 1;
		}
		else if (num > 1)
		{
			//Define loop controlling variable
			var temp: Int = num;
			var i: Int = 1;
			// Execute loop, until number is divisible by [i]
            while (temp % i == 0)
			{
				temp = temp / i;
				// Get next number
				i += 1;
			}
			if (temp == 1)
			{
				//When remainder of (i-1) is one 
				result = i - 1;
			}
		}
		if (result != 0)
		{
			print("\n [\(num)] is factorial of ", result, terminator: "");
		}
		else
		{
			print("\n [\(num)] is not factorial of any number", terminator: "");
		}
	}
}
func main()
{
	let obj: FactorialNo = FactorialNo();
	// Test Case
	obj.is_factorial_of(24);
	obj.is_factorial_of(2);
	obj.is_factorial_of(34);
	obj.is_factorial_of(720);
	obj.is_factorial_of(120);
	obj.is_factorial_of(0);
}
main();

Output

 [24] is factorial of  4
 [2] is factorial of  2
 [34] is not factorial of any number
 [720] is factorial of  6
 [120] is factorial of  5
 [0] is factorial of  1

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







© 2021, kalkicode.com, All rights reserved