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

Use C only not C++ and write a little describtion to each step Write a program t

ID: 3803830 • Letter: U

Question

Use C only not C++

and write a little describtion to each step

Write a program to calculate the factorial of a number. The factorial n! of a positive number (integer) is defined by n! = n * (n - 1)*(n - 2)*....*3*2*1, where 0! = 1. In function main prompt the user for a negative integer less than 15 for which the factorial is to be found. Use a while validation loop to assure the number entered is in the proper range. Then call a function to calculate the factorial. The input to the function is the integer n and the return value is n factorial. Use data type double for the factorial. In the function use a for loop to calculate the factorial. Make provision for the situation in which n = 0. Return the value of n factorial to function main and print. Test with n = 14.

Explanation / Answer

//header file for standard input output
#include <stdio.h>
//function definaiton to calculate the factorial
double calculateFactorial(int n){
int i;
double factorial = 1;//factorial with double data-type

if(n == 0) //if n == 0
factorial = 1;//foe n=0 factorial is 1

else
{
for(i=1; i<=n; ++i)
{
factorial *= i; //calculating factorial
}
}
return factorial;//return calculated factorial
}
//driver main function
int main()
{
int n = -1;//to store value of input
int flag = 1;
while(flag){
printf("Enter the value of n where (0 < n < 15) ");
scanf("%d",&n);
if(n > 0 && n < 15)
flag = 0;
}//provision to get another input if the value doesnt matches the range

double factorial = calculateFactorial(n); //call to the function to return the value of factorial of n
printf("The factorial of %d is %f. ",n,factorial); //displays factorial
return 0;
}

HOPE THIS HELPS

FEEL FREE TO COMMENT AND RATE

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote