Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C Programming Given that the mathematical formula for a Factorial calculation (w

ID: 3849151 • Letter: C

Question

C Programming

Given that the mathematical formula for a Factorial calculation (written n!) is: n! = n * (n - 10) * (n - 2) * (n - 3) * ..*1 where n is a positive integer (i) Write a C function called factorial, which: accepts an integer value n in the range 0 to 10 calculates n! using a loop and returns the result for the function as a suitably sized variable type. Note that 10! = 3628800 and 0! = 1 (ii) Briefly explain the lines of program which control the loop and determine how many times the loop runs. (iii) Briefly explain the lines of program which calculate the factorial value and why you chose a particular return value type.

Explanation / Answer

#include <stdio.h>
int main()
{
int n, i; //Initialization of variables 'i' is for loop 'n' is for standard input
unsigned long fact = 1; // Here we are initialising fact variable to 1

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

// show error if the user enters a value that more than 10.
if (n > 10 )
printf("Please enter the number from 1 to 10");
else
{
for(i=1; i<=n; ++i) // Here we are calculating the factorial (n!) by using 'for' loop and this loop will iterate from 1 to 'n' that we are given as an Input
{
// fact *= i; // Here we can calculate factorial in both the ways
fact = fact*i;
}
printf("Factorial of %d = %llu", n, fact); // Printing the output
}

return 0;
}