Factorial of a number

Here given code implementation process.

//C Program
//Find factorial of a number
//Recursively
#include <stdio.h>

//Return factorial number
int factorial(int number)
{
  if(number==0)
  {
    //Base case to control recursion
    return 1;
  }
  //Recursively calculate the factorial number
  return number*factorial(number-1);
}

//Method which are handle the request of factorial number
void factorial_no(int number)
{
  if(number < 0)
  {
    //When number is negative
    return;
  }
  printf("Factorial of %d is : %d\n",number,factorial(number) );
}
int main()

{
  //Test Case
  factorial_no(4); 
  factorial_no(5); 
  factorial_no(0); 
  factorial_no(3);

  return 0;
}

Output

Factorial of 4 is : 24
Factorial of 5 is : 120
Factorial of 0 is : 1
Factorial of 3 is : 6
/*
 C++ Program
 Find factorial of a number
 Recursively
*/
#include<iostream>

using namespace std;

class MyNumber {
	public:

		//Return factorial number
		int factorial(int number) {
			if (number == 0) {
				return 1;
			}
			return number *this->factorial(number - 1);
		}
	//Method which are handle the request of factorial number
	void factorial_no(int number) {
		if (number < 0) {
			return;
		}
		cout << "Factorial of " << number << " is : " << this->factorial(number) << "\n";
	}
};
int main() {
	MyNumber obj;
	//Test Case
	obj.factorial_no(4);
	obj.factorial_no(5);
	obj.factorial_no(0);
	obj.factorial_no(3);
	return 0;
}

Output

Factorial of 4 is : 24
Factorial of 5 is : 120
Factorial of 0 is : 1
Factorial of 3 is : 6
/*
  Java Program
  Find factorial of a number
  Recursively
*/
public class MyNumber {

  //Return factorial number
  public int factorial(int number)
  {
    if(number==0)
    {
      //Base case to control recursion
      return 1;
    }
    //Recursively calculate the factorial number
    return number*factorial(number-1);
  }

  //Method which are handle the request of factorial number
  public void factorial_no(int number)
  {
    if(number < 0)
    {
      //When number is negative
      return;
    }
    System.out.print("Factorial of "+number+" is : "+factorial(number)+"\n" );
  }


  public static void main(String[] args) {

    MyNumber obj = new MyNumber();

    //Test Case
    obj.factorial_no(4); 
    obj.factorial_no(5); 
    obj.factorial_no(0); 
    obj.factorial_no(3);
  }
}

Output

Factorial of 4 is : 24
Factorial of 5 is : 120
Factorial of 0 is : 1
Factorial of 3 is : 6
/*
  C# Program
  Find factorial of a number
  Recursively
*/
using System;
public class MyNumber {

	//Return factorial number
	public int factorial(int number) {
		if (number == 0) {
			//Base case to control recursion
			return 1;
		}
		//Recursively calculate the factorial number
		return number * factorial(number - 1);
	}

	//Method which are handle the request of factorial number
	public void factorial_no(int number) {
		if (number < 0) {
			//When number is negative
			return;
		}
		Console.Write("Factorial of " + number + " is : " + factorial(number) + "\n");
	}


	public static void Main(String[] args) {

		MyNumber obj = new MyNumber();

		//Test Case
		obj.factorial_no(4);
		obj.factorial_no(5);
		obj.factorial_no(0);
		obj.factorial_no(3);
	}
}

Output

Factorial of 4 is : 24
Factorial of 5 is : 120
Factorial of 0 is : 1
Factorial of 3 is : 6
# Python 3 Program
# Find factorial of a number
# Recursively
class MyNumber :
	# Return factorial number
	def factorial(self, number) :
		if (number == 0) :
			return 1
		
		return number * self.factorial(number - 1)
	
	# Method which are handle the request of factorial number
	def factorial_no(self, number) :
		if (number < 0) :
			return
		
		print("Factorial of ", number ," is : ", self.factorial(number) )
	

def main() :
	obj = MyNumber()
	# Test Case
	obj.factorial_no(4)
	obj.factorial_no(5)
	obj.factorial_no(0)
	obj.factorial_no(3)


if __name__ == "__main__":
	main()

Output

Factorial of 4 is : 24
Factorial of 5 is : 120
Factorial of 0 is : 1
Factorial of 3 is : 6
# Ruby Program 
# Find factorial of a number
# Recursively
class MyNumber 
	# Return factorial number
	def factorial(number) 
		if (number == 0) 
			return 1
		end
		return number * self.factorial(number - 1)
	end
	# Method which are handle the request of factorial number
	def factorial_no(number) 
		if (number < 0) 
			return
		end
		print("Factorial of ", number ," is  :", self.factorial(number) ,"\n")
	end
end
def main() 
	obj = MyNumber.new()
	# Test Case
	obj.factorial_no(4)
	obj.factorial_no(5)
	obj.factorial_no(0)
	obj.factorial_no(3)
end
main()

Output

Factorial of 4 is  :24
Factorial of 5 is  :120
Factorial of 0 is  :1
Factorial of 3 is  :6
/*
 Scala Program
 Find factorial of a number
 Recursively
*/
class MyNumber {
	//Return factorial number
	def factorial(number: Int): Int = {
		if (number == 0) {
			return 1;
		}
		return number * this.factorial(number - 1);
	}
	//Method which are handle the request of factorial number
	def factorial_no(number: Int): Unit = {
		if (number < 0) {
			return;
		}
		print("Factorial of " + number + " is : " + this.factorial(number) + "\n");
	}
}
object Main {
	def main(args: Array[String]): Unit = {
		var obj: MyNumber = new MyNumber();
		//Test Case
		obj.factorial_no(4);obj.factorial_no(5);obj.factorial_no(0);obj.factorial_no(3);
	}
}

Output

Factorial of 4 is : 24
Factorial of 5 is : 120
Factorial of 0 is : 1
Factorial of 3 is : 6
/*
  Swift 4 Program
  Find factorial of a number
  Recursively
*/
class MyNumber {
	//Return factorial number
	func factorial(_ number: Int) -> Int {
		if (number == 0) {
			return 1;
		}
		return number * self.factorial(number - 1);
	}
	//Method which are handle the request of factorial number
	func factorial_no(_ number: Int) {
		if (number < 0) {
			return;
		}
		print("Factorial of ", number ," is : ", self.factorial(number) );
	}
}
func main() {
	let obj: MyNumber = MyNumber();
	//Test Case
	obj.factorial_no(4);
	obj.factorial_no(5);
	obj.factorial_no(0);
	obj.factorial_no(3);
}
main();

Output

Factorial of  4  is :  24
Factorial of  5  is :  120
Factorial of  0  is :  1
Factorial of  3  is :  6
<?php
/*
  Php Program
  Find factorial of a number
  Recursively
*/
class MyNumber {
	//Return factorial number

	public 	function factorial($number) {
		if ($number == 0) {
			return 1;
		}
		return $number *$this->factorial($number - 1);
	}
	//Method which are handle the request of factorial number

	public 	function factorial_no($number) {
		if ($number < 0) {
			return;
		}
		echo("Factorial of ". $number ." is : ". $this->factorial($number) ."\n");
	}
};

function main() {
	$obj = new MyNumber();
	//Test Case

	$obj->factorial_no(4);
	$obj->factorial_no(5);
	$obj->factorial_no(0);
	$obj->factorial_no(3);
}
main();

Output

Factorial of 4 is : 24
Factorial of 5 is : 120
Factorial of 0 is : 1
Factorial of 3 is : 6
/*
 Node Js Program
 Find factorial of a number
 Recursively
*/
class MyNumber {
	//Return factorial number
	factorial(number) {
		if (number == 0) {
			return 1;
		}
		return number *this.factorial(number - 1);
	}
	//Method which are handle the request of factorial number
	factorial_no(number) {
		if (number < 0) {
			return;
		}
		process.stdout.write("Factorial of " + number + " is : " + this.factorial(number) + "\n");
	}
}

function main(args) {
	var obj = new MyNumber();
	//Test Case
	obj.factorial_no(4);
	obj.factorial_no(5);
	obj.factorial_no(0);
	obj.factorial_no(3);
}
main();

Output

Factorial of 4 is : 24
Factorial of 5 is : 120
Factorial of 0 is : 1
Factorial of 3 is : 6

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