Write a complete C++ program called Lab6C.cpp that computes and returns the fact
ID: 672718 • Letter: W
Question
Write a complete C++ program called Lab6C.cpp that computes and returns the factorial of a positive integer n, that is the product of all positive integers from 1 to n. Using n = 5, for example, we have 5! = 5 * 4 * 3 * 2 * 1 = 120. You will develop a function that will be passed an integer parameter, n, that is used to compute the factorial, and then return the integer factorial result to the calling function (that is, main). You must create both the function declaration and the function definition. Hint: You should use a loop to calculate the factorial of the positive integer. Note that the main() function will be responsible for prompting the user for and reading in a positive integer n, calling the factorial function, and then printing the result that is passed back to main to the screen in a meaningful message. You may assume that the user enters a positive integer.
Explanation / Answer
#include <stdio.h> //preprocessor directives
int fact(int) //function declaration
//Function for finding the facctorial of any integer number
int fact(int n) //function definition
{
if(n==0)
{
return 1;
}
return(n*fact(n-1));
}
int main()
{
int n;
printf(“Enter the value of n ”);
scanf(“%d” ,&n);//read a positive integer n
res=fact(n); //function call
printf(“The factorial of %d! = %d”,n,res); //printing result
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.