Factorial of a number through the ternary operator

Share Your Love

Factorial of a number through the ternary operator can evaluate in the same way as in a recursive way. Factorial of a number like 6 which is 720.

C++ program to find factorial of a given number through the ternary operator.

// C++ program to find factorial of given number
#include <iostream>

int factorial(int n)
{
	// single line to find factorial
	return (n == 1 || n == 0) ? 1 : n * factorial(n - 1);
}

// Driver Code
int main()
{
	int num = 5;
	printf("Factorial of %d is %d", num, factorial(num));
	return 0;
}

// This code is contributed by Rithika palaniswamy.

Java program to find factorial of a given number through the ternary operator

// Java program to find factorial
// of given number
class Factorial {

	int factorial(int n)
	{

		// single line to find factorial
		return (n == 1 || n == 0) ? 1 : n * factorial(n - 1);
	}

	// Driver Code
	public static void main(String args[])
	{
		Factorial obj = new Factorial();
		int num = 5;
		System.out.println(
			"Factorial of " + num
			+ " is " + obj.factorial(num));
	}
}

// This code is contributed by Anshika Goyal.

Python program to find factorial of a given number through the ternary operator

# Python 3 program to find
# factorial of given number

def factorial(n):

	# single line to find factorial
	return 1 if (n == 1 or n == 0) else n * factorial(n - 1)


# Driver Code
num = 5
print ("Factorial of", num, "is",
	factorial(num))

# This code is contributed
# by Smitha Dinesh Semwal.

C# program to find factorial of a given number through the ternary operator

// C# program to find factorial
// of the given number
using System;

class Factorial {

	int factorial(int n)
	{

		// single line to find factorial
		return (n == 1 || n == 0) ? 1 : n * factorial(n - 1);
	}

	// Driver Code
	public static void Main()
	{
		Factorial obj = new Factorial();
		int num = 5;

		Console.WriteLine(
			"Factorial of " + num
			+ " is " + obj.factorial(num));
	}
}

// This code is contributed by vt_m.

PHP program to find factorial of a given number through the ternary operator

<?php
// PHP program to find factorial
// of given number

function factorial( $n)
{
	
	// single line to find factorial
	return ($n == 1 || $n == 0) ? 1:
			$n * factorial($n - 1);
}

	// Driver Code
	$num = 5;
	echo "Factorial of ", $num, " is ", factorial($num);

// This code is contributed by anuj_67.
?>

Output:

Factorial of 5 is 120

If this article is helpful please share this article. And wants to improve this article please WhatsApp us.

Share Your Love
Avatar photo
Lingaraj Senapati

Hey There! I am Lingaraj Senapati, the Founder of lingarajtechhub.com My skills are Freelance, Web Developer & Designer, Corporate Trainer, Digital Marketer & Youtuber.

Articles: 411

Newsletter Updates

Enter your email address below to subscribe to our newsletter