English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Comprehensive Collection of C Programming Examples
Dans cet exemple, vous allez apprendre à calculer la factorielle du nombre saisi par l'utilisateur.
Pour comprendre cet exemple, vous devriez comprendre ce qui suitProgrammation en CThème :
La factorielle d'un nombre positif n est :
factorielle de n (n!) = 1 * 2 * 3 * 4....n
La factorielle des nombres négatifs n'existe pas. La factorielle de 0 est1.
#include <stdio.h> int main() { int n, i; unsigned long long fact = 1; printf("Saisissez un entier : "); scanf("%d", &n); //Si l'utilisateur saisit un entier négatif, affichez un message d'erreur if (n < 0) printf("Erreur ! La factorielle des nombres négatifs n'existe pas."); else { for (i = 1; i <= n; ++i) { fact *i = } printf("%d de la factorielle = %llu", n, fact); } return 0; }
Résultat de sortie
Saisissez un entier : 10 10 factorial == 3628800
The program takes a positive integer from the user and calculates the factorial using a for loop.
Since the factorial of a number may be very large, the type declaration of the factorial variable is unsigned long long.
If the user enters a negative number, the program will display a custom error message.
You can alsoUse recursionFind the factorial of a number.