Calculate the factorial N! = 123 ... (N-1) N of a natural integer N respecting that 0! = 1.


Difficulty level
Video recording
This exercise is mostly suitable for students
#include <stdio.h>
#include <conio.h>

void main()
{
	int  N;      
 	int  I;      /* counter */
 	double FACT; /* double since the result could grow beyond int */

 	do
    	{
      		printf("Enter an integer : ");
     		scanf("%d", &N);
    	}while (N<0);
 
 	/* solution using while loops */
 	/* Pour N=0, the result is 0!=1 */
 	I=1;
 	FACT=1;
 	while (I<=N)
       	{
        	FACT*=I;
        	I++;
       	}
 
	/* solution using fpre loops */
 	/*
 	for (FACT=1.0, I=1 ; I<=N ; I++)
        	FACT*=I;
  	*/

 	printf ("%d! = %.0f\n", N, FACT);

	getch();
}

Back to the list of exercises
Looking for a more challenging exercise, try this one !!
2 players Game - version 1