Write an iterative function that calculates the factorial of a positive integer.


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


int factit(int n)
{
  	int f;				// Result

  	for (f = 1; n > 1; n--)
    		f *= n;	
  	return f;
}
 
void main()
{
  	int n;


  	printf("n : ");
  	scanf("%d", &n);

  	printf("%d!=%d\n",n,factit(n));

	getch();
}

Back to the list of exercises
Looking for a more challenging exercise, try this one !!
Lagrange form of the polynomial interpolation