In: Computer Science
Write a parameterized function that takes in a file name as a parameter, reads the file, calculates the factorial of each number, and displays a formatted output as follows:
Factorial of 10 = 3628800 Factorial of 5 = 120
Short Summary:
**************Please upvote the answer and appreciate our time.************
Source Code:
#include <stdio.h>
// Function to find factorial of given number
unsigned int factorial(unsigned int n)
{
if (n == 0)
return 1;
return n * factorial(n - 1);
}
int main()
{
// file to read the integers
FILE* file = fopen ("input.txt", "r");
int i = 0;
// read first integer
fscanf (file, "%d", &i);
// continue to read till end of file
while (!feof (file))
{
//Calculate factorial
printf ("Factorial of %d: %d\n", i, factorial(i));
// read the next integer
fscanf (file, "%d", &i);
}
// clost the input file
fclose (file);
return 0;
}
Refer the following screenshots for code indentation:
Sample Run:
Iinput.txt file:
output:
**************************************************************************************
Feel free to rate the answer and comment your questions, if you have any.
Please upvote the answer and appreciate our time.
Happy Studying!!!
**************************************************************************************