Write a C program to calculate the factorial of a given number using loops. Includes complete code, explanation, and example output.