Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- //#include<conio.h>
- double fact_(int n);
- double fact_rec(int n);
- int main(){
- int n,i;
- double fact_for = 1,fact_while = 1;
- printf("Donner un nombre pour calculer son factorielle :");
- do{
- if(scanf("%d",&n)!=1){
- printf("Erreur : Invalide Input.\n");
- }
- }while(n < 0);
- //la boucle for i--
- for(i=n;i;i--){
- fact_for *= i;
- }
- printf("la factorielle de %d est : %.0lf\n",n,fact_for);
- //la boucle for i++
- fact_for = 1;
- for(i = 1;i <= n;i++){
- fact_for *= i;
- }
- printf("la factorielle de %d est : %.0lf\n",n,fact_for);
- //la boucle while i++
- i= 1;
- while(i <= n){
- fact_while *= i;
- i++;
- }
- printf("la factorielle de %d est : %.0lf\n",n,fact_while);
- //la boucle while i--
- i = n;
- fact_while = 1;
- while(i){
- fact_while *= i;
- i--;
- }
- printf("la factorielle de %d est : %.0lf\n",n,fact_while);
- //les fonctions
- //recursive
- printf("la factorielle de %d est : %.0lf\n",n,fact_rec(n));
- //simple
- printf("la factorielle de %d est : %.0lf\n",n,fact_(n));
- //getch();
- return 0;
- }
- //fonction factorielle
- double fact_(int n){
- double fact = 1;
- int i = n;
- while(i){
- fact *= i;
- i--;
- }
- return fact;
- }
- //fonction recursive
- double fact_rec(int n){
- double fact = 1.;
- if(n){
- fact = (n * fact_rec(n-1));
- }
- return fact;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement