Write a C program NOT C++ Write a C program to calculate the factorial of a numb
ID: 3834414 • Letter: W
Question
Write a C program NOT C++
Write a C 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 non-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 die 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
main.c
#include <stdio.h>
double multiplyNumbers(double n);
int main()
{
double number;
double valid = 0;
while( valid == 0 ) {
printf("Enter a number between 1 and 15 : ");
scanf("%lf", &number );
if( (number < 1) || (number > 15) )
printf("Number is outside legal range ");
else
printf(" Factorial of %f = %f", number, multiplyNumbers(number));
valid = 1;
}
return 0;
}
double multiplyNumbers(double n)
{
double factorial = 1;
int i;
for(i=1; i<=n; ++i)
{
factorial *= i; // factorial = factorial*i;
}
return factorial;
}
Output:-
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.