Question

In: Computer Science

Write a parameterized function that takes in a file name as a parameter, reads the file,...

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 

Solutions

Expert Solution

Short Summary:

  • Since you did not mention the programming language to be used, I used C programming and shown you sample run,

**************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!!!

**************************************************************************************


Related Solutions

Write a function called fnReadInParameters() that takes as parameter(s): • a string indicating the name of...
Write a function called fnReadInParameters() that takes as parameter(s): • a string indicating the name of the parameter le This function should return a dictionary that stores all of the parameter le data of the SIR model in a format that we will describe further below.
C++ Write a program that prompts for a file name and then reads the file to...
C++ Write a program that prompts for a file name and then reads the file to check for balanced curly braces, {; parentheses, (); and square brackets, []. Use a stack to store the most recent unmatched left symbol. The program should ignore any character that is not a parenthesis, curly brace, or square bracket. Note that proper nesting is required. For instance, [a(b]c) is invalid. Display the line number the error occurred on. These are a few of the...
Python Programming 1. Write a function name split_name which takes one parameter called name. If the...
Python Programming 1. Write a function name split_name which takes one parameter called name. If the name parameter value is a name in the form LastName, FirstName(e.g., ”Grounds, Nic”) then the function should return a list of two elements where FirstName is the first element and LastName is the second element. If the name parameter value is a name in the form FirstName LastName(e.g., ”Nic Grounds”) then the function should return a list of two elements where FirstName is the...
In the same module/file, write a function processAnimals that takes one argument, the name of an...
In the same module/file, write a function processAnimals that takes one argument, the name of an input file.   The function returns the list of animals created or an empty list if the file didn't contain any lines. The input file contains zero or more lines with the format: species, language, age where species is a string representing an animal's species, language is a string representing an animal's language, and age is an integer representing an animal's age. The items on...
Write a function redact() takes as input a file name. See secret.txt for the initial test....
Write a function redact() takes as input a file name. See secret.txt for the initial test. The function should print the contents of the file on the screen with this modification: Every occurrence of string 'secret' in the file should be replaced with string 'xxxxxx'. Every occurrence of “agent’ should be replaced with ‘yyyyyy’. Every occurrence of carrier should be replaced with ‘zzzzzz’. This function should catch exceptions. FOR PYTHON
Write a .m function file on MATLAB that reads the parameters stored in the file missile...
Write a .m function file on MATLAB that reads the parameters stored in the file missile data.txt into MATLAB. The function should have the following declaration: function [X0, Y0, Z0, m0, mf, Thmag0, theta, phi, Tburn] = read input( input filename, M id) where input filename is a string variable denoting the name of the file to be read and M_id is an integer which denotes the missile ID. The outputs are the initial position (X0, Y0, Z0), initial and...
Write a python code to Design and implement a function with no input parameter which reads...
Write a python code to Design and implement a function with no input parameter which reads a number from input (like 123). Only non-decimal numbers are valid (floating points are not valid). The number entered by the user should not be divisible by 10 and if the user enters a number that is divisible by 10 (like 560), it is considered invalid and the application should keep asking until the user enters a valid input. Once the user enters a...
Write a function that takes a C string as an input parameter and reverses the string.
in c++ Write a function that takes a C string as an input parameter and reverses the string. The function should use two pointers, front and rear. The front pointer should initially reference the first character in the string, and the rear pointer should initially reference the last character in the string. Reverse the string by swapping the characters referenced by front and rear, then increment front to point to the next character and decrement rear to point to the...
in c++ Write a function that takes a C string as an input parameter and reverses...
in c++ Write a function that takes a C string as an input parameter and reverses the string. The function should use two pointers, front and rear. The front pointer should initially reference the first character in the string, and the rear pointer should initially reference the last character in the string. Reverse the string by swapping the characters referenced by front and rear, then increment front to point to the next character and decrement rear to point to the...
C++ Write a function called linearSearch that takes an array as a parameter and search for...
C++ Write a function called linearSearch that takes an array as a parameter and search for a specific value inside this parameter. The function returns the frequency of a specific value in the array. In the main function: 1. Define an array called salaries of length 5. 2. Initialize the array by asking the user to input the values of its elements. 3. Define a variable called key and ask the user to enter a value for this variable. 4....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT