logo

Programme pour factorielle d'un nombre

Quelle est la factorielle d'un nombre ?

  • La factorielle d'un entier non négatif est la multiplication de tous les entiers positifs inférieurs ou égaux à n. Par exemple, la factorielle de 6 est 6*5*4*3*2*1, soit 720.
  • Une factorielle est représentée par un nombre et un ! marque à la fin. Il est largement utilisé dans les permutations et les combinaisons pour calculer le total des résultats possibles. Le mathématicien français Christian Kramp a été le premier à utiliser l'exclamation.

Factoriel de pratique recommandée Essayez-le !

Créons un programme factoriel utilisant des fonctions récursives. Jusqu'à ce que la valeur ne soit pas égale à zéro, la fonction récursive s'appellera. La factorielle peut être calculée à l'aide de la formule récursive suivante.

n! = n * (n – 1) !
n! = 1 si n = 0 ou n = 1



Ci-dessous la mise en œuvre :

C++




// C++ program to find> // factorial of given number> #include> using> namespace> std;> > // Function to find factorial> // of given number> unsigned>int> factorial(unsigned>int> n)> > >if> (n == 0> > // Driver code> int> main()> {> >int> num = 5;> >cout <<>'Factorial of '> ><< num <<>' is '> << factorial(num) << endl;> >return> 0;> }> // This code is contributed by Shivi_Aggarwal>

>

parcourir la carte Java
>

C




// C program to find factorial of given number> #include> > // function to find factorial of given number> unsigned>int> factorial(unsigned>int> n)> {> >if> (n == 0)> >return> 1;> >return> n * factorial(n - 1);> }> > int> main()> {> >int> num = 5;> >printf>(>'Factorial of %d is %d'>, num, factorial(num));> >return> 0;> }>

>

>

Java




// Java program to find factorial of given number> class> Test {> >// method to find factorial of given number> >static> int> factorial(>int> n)> >{> >if> (n ==>0>)> >return> 1>;> > >return> n * factorial(n ->1>);> >}> > >// Driver method> >public> static> void> main(String[] args)> >{> >int> num =>5>;> >System.out.println(>'Factorial of '> + num> >+>' is '> + factorial(>5>));> >}> }>

>

>

Python3




# Python 3 program to find> # factorial of given number> > # Function to find factorial of given number> def> factorial(n):> > >if> n>=>=> 0>:> >return> 1> > >return> n>*> factorial(n>->1>)> > # Driver Code> num>=> 5>;> print>(>'Factorial of'>, num,>'is'>,> factorial(num))> > # This code is contributed by Smitha Dinesh Semwal>

>

>

C#




// C# program to find factorial> // of given number> using> System;> > class> Test {> >// method to find factorial> >// of given number> >static> int> factorial(>int> n)> >{> >if> (n == 0)> >return> 1;> > >return> n * factorial(n - 1);> >}> > >// Driver method> >public> static> void> Main()> >{> >int> num = 5;> >Console.WriteLine(>'Factorial of '> >+ num +>' is '> + factorial(5));> >}> }> > // This code is contributed by vt_m>

>

>

PHP




// PHP program to find factorial // of given number // function to find factorial // of given number function factorial($n) { if ($n == 0) return 1; return $n * factorial($n - 1); } // Driver Code $num = 5; echo 'Factorial of ', $num, ' is ', factorial($num); // This code is contributed by m_kit ?>>

>

>

Javascript




> // Javascript to find factorial> // of given number> > // function to find factorial> // of given number> function> factorial(n) {> >if> (n == 0)>return> 1;> >return> n * factorial(n - 1);> }> > // Driver Code> let num = 5;> document.write(>'Factorial of '> + num +>' is '> + factorial(num));> > // This code is contributed by Saurabh Jaiswal> > >

>

>

Sortir

Factorial of 5 is 120>

Complexité temporelle : Sur)
Espace auxiliaire : Sur)

Solution itérative pour trouver la factorielle d'un nombre :

La factorielle peut également être calculée de manière itérative car la récursivité peut être coûteuse pour les grands nombres. Ici, nous avons montré l'approche itérative utilisant à la fois les boucles for et while.

Approche 1 : Utilisation de la boucle For

Suivez les étapes pour résoudre le problème :

  • À l’aide d’une boucle for, nous écrirons un programme permettant de trouver la factorielle d’un nombre.
  • Une variable entière avec une valeur de 1 sera utilisée dans le programme.
  • À chaque itération, la valeur augmentera de 1 jusqu'à ce qu'elle soit égale à la valeur saisie par l'utilisateur.
  • La factorielle du nombre saisi par l'utilisateur sera la valeur finale dans la variable de fait.

Vous trouverez ci-dessous la mise en œuvre de l'approche ci-dessus :

C++




// C++ program for factorial of a number> #include> using> namespace> std;> > // function to find factorial of given number> unsigned>int> factorial(unsigned>int> n)> {> >int> res = 1, i;> >for> (i = 2; i <= n; i++)> >res *= i;> >return> res;> }> > // Driver code> int> main()> {> >int> num = 5;> >cout <<>'Factorial of '> ><< num <<>' is '> ><< factorial(num) << endl;> >return> 0;> }> > // This code is contributed by Shivi_Aggarwal>

>

>

C




#include> > // function to find factorial of given number> unsigned>int> factorial(unsigned>int> n)> {> >int> res = 1, i;> >for> (i = 2; i <= n; i++)> >res *= i;> >return> res;> }> > int> main()> {> >int> num = 5;> >printf>(> >'Factorial of %d is %d'>, num, factorial(num));> >return> 0;> }>

Java remplace le caractère dans la chaîne
>

>

Java




// Java program to find factorial of given number> class> Test {> > >// Method to find factorial of the given number> >static> int> factorial(>int> n)> >{> >int> res =>1>, i;> >for> (i =>2>; i <= n; i++)> >res *= i;> >return> res;> >}> > >// Driver method> >public> static> void> main(String[] args)> >{> >int> num =>5>;> >System.out.println(> >'Factorial of '> + num> >+>' is '> + factorial(>5>));> >}> }>

>

>

Python3




# Python 3 program to find> # factorial of given number> > # Function to find factorial of given number> def> factorial(n):> > >res>=> 1> > >for> i>in> range>(>2>, n>+>1>):> >res>*>=> i> >return> res> > # Driver Code> num>=> 5>;> print>(>'Factorial of'>, num,>'is'>,> factorial(num))> > # This code is contributed by Smitha Dinesh Semwal>

>

>

C#




// C# program to find> // factorial of given number> using> System;> > class> Test {> >// Method to find factorial> >// of given number> >static> int> factorial(>int> n)> >{> >int> res = 1, i;> > >for> (i = 2; i <= n; i++)> >res *= i;> >return> res;> >}> > >// Driver method> >public> static> void> Main()> >{> >int> num = 5;> >Console.WriteLine(> >'Factorial of '> + num> >+>' is '> + factorial(5));> >}> }> > // This code is contributed by vt_m>

>

>

PHP




// function to find factorial // of given number function factorial( $n) { $res = 1; $i; for ($i = 2; $i <= $n; $i++) $res *= $i; return $res; } // Driver Code $num = 5; echo 'Factorial of ', $num, ' is ', factorial($num); // This code is contributed // by anuj_67. ?>>

>

>

Javascript




> // JavaScript program to find factorial of given number> > >// Method to find factorial of the given number> >function> factorial(n)> >{> >var> res = 1, i;> >for> (i = 2; i <= n; i++)> >res *= i;> >return> res;> >}> > >// Driver method> > >var> num = 5;> >document.write(>'Factorial of '> + num +>' is '> + factorial(5));> > > // This code is contributed by shivanisinghss2110.> > >

>

>

Sortir

Factorial of 5 is 120>

Complexité temporelle : Sur)
Espace auxiliaire : O(1)

Approche 2 : Cet exemple utilise une boucle while pour implémenter l'algorithme et trouver le programme factoriel.

C




// C program for factorial of a number> #include> > // function to find factorial of given number> unsigned>int> factorial(unsigned>int> n)> {> >if>(n == 0)> >return> 1;> >int> i = n, fact = 1;> >while> (n / i != n) {> >fact = fact * i;> >i--;> >}> >return> fact;> }> > int> main()> {> >int> num = 5;> >printf>(>'Factorial of %d is %d'>, num, factorial(num));> >return> 0;> }>

>

>

C++




// C++ program for factorial of a number> #include> using> namespace> std;> > // function to find factorial of given> // number using while loop> unsigned>int> factorial(unsigned>int> n)> {> >if>(n == 0)> >return> 1;> >int> i = n, fact = 1;> >while> (n / i != n) {> >fact = fact * i;> >i--;> >}> >return> fact;> }> > // Driver code> int> main()> {> >int> num = 5;> >cout <<>'Factorial of '> ><< num <<>' is '> ><< factorial(num) << endl;> >return> 0;> }> // This code is contributed by Shivi_Aggarwal>

tutoriel javascript
>

>

Java




// Java program to find factorial of given number> > class> Test {> > >// Method to find factorial of the given number> >static> int> factorial(>int> n)> >{> >if>(n ==>0>)> >return> 1>;> >int> i = n, fact =>1>;> >while> (n / i != n) {> >fact = fact * i;> >i--;> >}> >return> fact;> >}> > >// Driver method> >public> static> void> main(String[] args)> >{> >int> num =>5>;> >System.out.println(> >'Factorial of '> + num> >+>' is '> + factorial(>5>));> >}> }>

>

>

Python3




# Python 3 program to find> # factorial of given number> > # Function to find factorial of given number> def> factorial(n):> >if>(n>=>=> 0>):> >return> 1> >i>=> n> >fact>=> 1> > >while>(n>/> i !>=> n):> >fact>=> fact>*> i> >i>->=> 1> > >return> fact> > # Driver Code> num>=> 5>;> print>(>'Factorial of'>, num,>'is'>,> factorial(num))> > # This code is contributed by Smitha Dinesh Semwal>

>

>

C#




// C# program to find> // factorial of given number> using> System;> > class> Test {> >// Method to find factorial> >// of given number> >static> int> factorial(>int> n)> >{> >if>(n == 0)> >return> 1;> >int> i = n, fact = 1;> >while> (n / i != n) {> >fact = fact * i;> >i--;> >}> >return> fact;> >}> > >// Driver method> >public> static> void> Main()> >{> >int> num = 5;> >Console.WriteLine(> >'Factorial of '> + num> >+>' is '> + factorial(5));> >}> }>

>

>

Javascript




> >// JavaScript Program to implement> >// the above approach> >// function to find factorial of given> >// number using while loop> >function> factorial(n) {> >if> (n == 0)> >return> 1;> >let i = n, fact = 1;> >while> (Math.floor(n / i) != n) {> >fact = fact * i;> >i--;> >}> >return> fact;> >}> > >// Driver code> >let num = 5;> >document.write(>'Factorial of '> >+ num +>' is '> >+ factorial(num) +>' '>);> > // This code is contributed by Potta Lokesh> > >>

>

>

Sortir

Factorial of 5 is 120>

Complexité temporelle : SUR)
Espace auxiliaire : O(1)

Approche 3 : UN opérateur ternaire peut être considéré comme un raccourci pour une instruction if…else. Les conditions sont fournies, ainsi que les déclarations à exécuter sur cette base. Voici le programme pour factorielle utilisant un opérateur ternaire.

C++




// C++ program to find factorial of given number> #include> using> namespace> std;> > int> factorial(>int> n)> > >// single line to find factorial> >return> (n == 1> > // Driver Code> int> main()> {> >int> num = 5;> >cout <<>'Factorial of '> << num <<>' is '><< factorial(num);> >return> 0;> }> > // This code is contributed by shivanisinghss2110>

>

>

C




polymorphisme java
// C++ program to find factorial of given number> #include> > int> factorial(>int> n)> 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




// Java program to find factorial> // of given number> class> Factorial {> > >int> factorial(>int> n)> > 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.>

>

>

Python3




# 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#




// C# program to find factorial> // of the given number> using> System;> > class> Factorial {> > >int> factorial(>int> n)> >> > >// 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




// PHP program to find factorial // of given number function factorial( $n) $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. ?>>

>

>

Javascript




> > // JavaScript program to find factorial of given number> function> factorial(n)> > > // Driver Code> > >var> num = 5;> >document.write(>'Factorial of '> +num +>' is '> + factorial(num));> > // This code is contributed by shivanisinghss2110.> > >

>

>

Sortir

commande de retour java
Factorial of 5 is 120>

Complexité temporelle : Sur)
Espace auxiliaire : Sur)

Problèmes d'écriture du code de factorielle

Lorsque la valeur de n change augmente de 1, la valeur de la factorielle augmente de n. Ainsi, la variable stockant la valeur de la factorielle doit avoir une grande taille. Voici la valeur de n dont la factorielle peut être stockée dans la taille respective.

1. entier -> n<=12

2. long long int -> n<=19

À partir des données ci-dessus, nous pouvons voir qu’une très petite valeur de n peut être calculée en raison de la croissance plus rapide de la fonction factorielle. Nous pouvons cependant trouver la valeur mod de la factorielle de valeurs plus grandes en prenant mod à chaque étape.

La solution ci-dessus provoque un débordement pour un grand nombre. Veuillez vous référer à la factorielle d'un grand nombre pour une solution qui fonctionne pour les grands nombres.
Veuillez écrire des commentaires si vous trouvez un bug dans le code/algorithme ci-dessus, ou trouvez d'autres moyens de résoudre le même problème.