Factorial of a number

Factorial number program in java

/*
  Java program for
  Find factorial of a number Recursively
*/
public class Numbers
{
    // 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  
    // Calculate factorial number.
    public void finFactorial(int number)
    {
        if (number < 0)
        {
            // When number is negative
            return;
        }
        // Display result
        System.out.println("Factorial of " + 
                           number + " is : " + 
                           factorial(number) );
    }
    public static void main(String[] args)
    {
        Numbers task = new Numbers();
        // Test Case
        task.finFactorial(4);
        task.finFactorial(5);
        task.finFactorial(0);
        task.finFactorial(3);
    }
}
Output
Factorial of 4 is : 24
Factorial of 5 is : 120
Factorial of 0 is : 1
Factorial of 3 is : 6

Factorial number program in c++

// Include header file
#include <iostream>

//  Stdc++11 program for
//  Find factorial of a number Recursively
class Numbers
{
    // 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
    // Calculate factorial number.
    void finFactorial(int number)
    {
        if (number < 0)
        {
            // When number is negative
            return;
        }
        // Display result
        std::cout << "Factorial of " 
                  << number
                  << " is : " 
                  << factorial(number) << std::endl;
    }
};
int main(int argc, char **argv){
    Numbers task ;
    // Test Case
    task.finFactorial(4);
    task.finFactorial(5);
    task.finFactorial(0);
    task.finFactorial(3);
    return 0;
};
Output
Factorial of 4 is : 24
Factorial of 5 is : 120
Factorial of 0 is : 1
Factorial of 3 is : 6

Factorial number program in c

// Include header file
#include <stdio.h>

//  C program for
//  Find factorial of a number Recursively

// 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
// Calculate factorial number.
void  finFactorial (int number)
{
    if (number < 0)
    {
        // When number is negative
        return;
    }
    // Display result
    printf("Factorial of %d is : %d\n" ,number, 
        factorial(number));
}

int main()
{

    // Test Case
    finFactorial(4);
    finFactorial(5);
    finFactorial(0);
    finFactorial(3);
    return 0; 
}
Output
Factorial of 4 is : 24
Factorial of 5 is : 120
Factorial of 0 is : 1
Factorial of 3 is : 6

Factorial number program in golang

package main
import "fmt"
//  Go lang program for
//  Find factorial of a number Recursively
// Return factorial number
func factorial(number int) int {
	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
// Calculate factorial number.
func finFactorial(number int) {
	if (number < 0) {
		// When number is negative
		return;
	}
	// Display result
	fmt.Printf("Factorial of %d is : %d\n", number, factorial(number));
}
func main() {
	// Test Case
	finFactorial(4);
	finFactorial(5);
	finFactorial(0);
	finFactorial(3);
}
Output
Factorial of 4 is : 24
Factorial of 5 is : 120
Factorial of 0 is : 1
Factorial of 3 is : 6

Factorial number program in c#

// Include namespace system
using System; 
//  C# program for
//  Find factorial of a number Recursively
public class Numbers
{
    // 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 * this.factorial(number - 1);
    }
    // Method which are handle the request of
    // Calculate factorial number.
    public void finFactorial(int number)
    {
        if (number < 0)
        {
            // When number is negative
            return;
        }
        // Display result
        Console.WriteLine("Factorial of " + 
                          number + " is : " + 
                          this.factorial(number));
    }
    public static void Main(String[] args)
    {
        var task = new Numbers();
        // Test Case
        task.finFactorial(4);
        task.finFactorial(5);
        task.finFactorial(0);
        task.finFactorial(3);
    }
}
Output
Factorial of 4 is : 24
Factorial of 5 is : 120
Factorial of 0 is : 1
Factorial of 3 is : 6

Factorial number program in vb.net

' Include namespace system
Imports System 
'  Vb.net program for
'  Find factorial of a number Recursively
public Class Numbers
    ' Return factorial number
    Public Function  factorial(ByVal number As Integer) As Integer
        if (number = 0) Then
            ' Base case to control recursion
            Return  1
        End If
        ' Recursively calculate the factorial number
        Return  number * Me.factorial(number - 1)
    End Function
    ' Method which are handle the request of
    ' Calculate factorial number.
    Public Sub finFactorial(ByVal number As Integer)
        if (number < 0) Then
            ' When number is negative
            Return
        End If
        ' Display result
        Console.WriteLine("Factorial of {0} is : {1}", 
                          number, Me.factorial(number))
    End Sub
    Public Shared Sub Main(ByVal args As String())
        Dim task As Numbers = New Numbers()
        ' Test Case
        task.finFactorial(4)
        task.finFactorial(5)
        task.finFactorial(0)
        task.finFactorial(3)
    End Sub
End Class
Output
Factorial of 4 is : 24
Factorial of 5 is : 120
Factorial of 0 is : 1
Factorial of 3 is : 6

Factorial number program in php

<?php 
//  Php program for
//  Find factorial of a number Recursively
class Numbers
{
    // Return factorial number
    function factorial($number)
    {
        if ($number == 0)
        {
            // Base case to control recursion
            return 1;
        }
        // Recursively calculate the factorial number
        return $number * $this->factorial($number - 1);
    }
    // Method which are handle the request of
    // Calculate factorial number.
    function finFactorial($number)
    {
        if ($number < 0)
        {
            // When number is negative
            return;
        }
        // Display result
        echo "Factorial of $number is : ",
             $this->factorial($number),"\n";
    }
}
$task = new Numbers();
// Test Case
$task->finFactorial(4);
$task->finFactorial(5);
$task->finFactorial(0);
$task->finFactorial(3);
Output
Factorial of 4 is : 24
Factorial of 5 is : 120
Factorial of 0 is : 1
Factorial of 3 is : 6

Factorial number program in node js

//  Node Js program for
//  Find factorial of a number Recursively
class Numbers
{
    // Return factorial number
    factorial(number)
    {
        if (number == 0)
        {
            // Base case to control recursion
            return 1;
        }
        // Recursively calculate the factorial number
        return number * this.factorial(number - 1);
    }
    // Method which are handle the request of
    // Calculate factorial number.
    finFactorial(number)
    {
        if (number < 0)
        {
            // When number is negative
            return;
        }
        // Display result
        console.log("Factorial of " + 
                    number + " is : " + 
                    this.factorial(number));
    }
}

 // Start program execution
var task = new Numbers();
// Test Case
task.finFactorial(4);
task.finFactorial(5);
task.finFactorial(0);
task.finFactorial(3);
Output
Factorial of 4 is : 24
Factorial of 5 is : 120
Factorial of 0 is : 1
Factorial of 3 is : 6

Factorial number program in typescript

// Typescript program for
// Find factorial of a number Recursively
class Numbers
{
    // Return factorial number
    public number factorial(number:number)
    {
        if (number == 0)
        {
            // Base case to control recursion
            return 1;
        }
        // Recursively calculate the factorial number
        return number * this.factorial(number - 1);
    }
    // Method which are handle the request of
    // Calculate factorial number.
    public  finFactorial(number:number)
    {
        if (number < 0)
        {
            // When number is negative
            return;
        }
        // Display result
        console.log("Factorial of " + 
            number + " is : " + 
            this.factorial(number));
    }
}
var task = new Numbers();
// Test Case
task.finFactorial(4);
task.finFactorial(5);
task.finFactorial(0);
task.finFactorial(3);
/*
 file : code.ts
 tsc --target es6 code.ts
 node code.js
 */
Output
Factorial of 4 is : 24
Factorial of 5 is : 120
Factorial of 0 is : 1
Factorial of 3 is : 6

Factorial number program in python

#  Python 3 program for
#  Find factorial of a number Recursively
class Numbers :
    # Return factorial number
    def  factorial(self, number) :
        if (number == 0) :
            # Base case to control recursion
            return 1
        # Recursively calculate the factorial number
        return number * self.factorial(number - 1)
    # Method which are handle the request of
    # Calculate factorial number.
    def finFactorial(self, number) :
        if (number < 0) :
            # When number is negative
            return
        # Display result
        print("Factorial of",number,"is :",
            self.factorial(number))

if __name__=="__main__":
    task = Numbers()
    # Test Case
    task.finFactorial(4)
    task.finFactorial(5)
    task.finFactorial(0)
    task.finFactorial(3)
Output
Factorial of 4 is : 24
Factorial of 5 is : 120
Factorial of 0 is : 1
Factorial of 3 is : 6

Factorial number program in ruby

#  Ruby program for
#  Find factorial of a number Recursively
class Numbers
    # Return factorial number
    def factorial( number)
        if (number == 0)
            # Base case to control recursion
            return 1
        end
        # Recursively calculate the factorial number
        return number * self.factorial(number - 1)
    end
    # Method which are handle the request of
    # Calculate factorial number.
    def finFactorial( number)
        if (number < 0)
            # When number is negative
            return
        end
        # Display result
        print("Factorial of ",
            number," is : ",
            self.factorial(number),"\n")
    end
end
task = Numbers.new()
# Test Case
task.finFactorial(4)
task.finFactorial(5)
task.finFactorial(0)
task.finFactorial(3)
Output
Factorial of 4 is : 24
Factorial of 5 is : 120
Factorial of 0 is : 1
Factorial of 3 is : 6

Factorial number program in scala

//  Scala program for
//  Find factorial of a number Recursively
class Numbers ()
{
    // Return factorial number
    def factorial(number : Int) : Int=
    {
        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
    // Calculate factorial number.
    def finFactorial(number : Int) : Unit=
    {
        if (number < 0)
        {
            // When number is negative
            return
        }
        // Display result
        println("Factorial of " + number + 
                " is : " + factorial(number))
    }
}

object Main 
{
	def main(args : Array[String]) : Unit=
    {
        var task = new Numbers()
        // Test Case
        task.finFactorial(4)
        task.finFactorial(5)
        task.finFactorial(0)
        task.finFactorial(3)
    }
}
Output
Factorial of 4 is : 24
Factorial of 5 is : 120
Factorial of 0 is : 1
Factorial of 3 is : 6

Factorial number program in swift

import Foundation
//  Swift program for
//  Find factorial of a number Recursively
class Numbers
{
    // Return factorial number
    func factorial(_ number : Int) -> Int
    {
        if (number == 0)
        {
            // Base case to control recursion
            return 1;
        }
        // Recursively calculate the factorial number
        return number * self.factorial(number - 1);
    }
    // Method which are handle the request of
    // Calculate factorial number.
    func finFactorial(_ number : Int)
    {
        if (number < 0)
        {
            // When number is negative
            return;
        }
        // Display result
        print("Factorial of",number,
            "is :",self.factorial(number));
    }
}

let task : Numbers = Numbers();
// Test Case
task.finFactorial(4);
task.finFactorial(5);
task.finFactorial(0);
task.finFactorial(3);
Output
Factorial of 4 is : 24
Factorial of 5 is : 120
Factorial of 0 is : 1
Factorial of 3 is : 6

Factorial number program in kotlin

//  Kotlin program for
//  Find factorial of a number Recursively
class Numbers {
    // Return factorial number
    fun factorial(number : Int) : Int
    {
        if (number == 0)
        {
            // Base case to control recursion
            return 1;
        }
        // Recursively calculate the factorial number
        return number * this.factorial(number - 1);
    }
    // Method which are handle the request of
    // Calculate factorial number.
    fun finFactorial(number : Int) : Unit
    {
        if (number < 0)
        {
            // When number is negative
            return;
        }
        // Display result
        println("Factorial of " + number + 
            " is : " + this.factorial(number));
    }
}
fun main(args : Array<String>) : Unit
{
    val task : Numbers = Numbers();
    // Test Case
    task.finFactorial(4);
    task.finFactorial(5);
    task.finFactorial(0);
    task.finFactorial(3);
}
Output
Factorial of 4 is : 24
Factorial of 5 is : 120
Factorial of 0 is : 1
Factorial of 3 is : 6



Comment

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







© 2022, kalkicode.com, All rights reserved