Language: \"C programming\" please read it carefully and make sure the code is r
ID: 3587045 • Letter: L
Question
Language: "C programming"
please read it carefully and make sure the code is running by doing what's inside the sample output.
thanks,
1. Factorial (fact.c) The factorial of a positive integer is defined as n! nn -1n 2.1 and by definition 0! Write a program that prompts the user to enter an integer number and uses a function, factorial, described below, to calculate the factorial of that number. Implement this function: long factorial(int n) Sample Output: Number to calculate for: 5 5!120 Data Type Requirement: Input data for the factorial function is of type int, output is of type long. Input Validation: Input value must be greater than equal to 0Explanation / Answer
//fact.c
#include<stdio.h>
long factorial(int n)
{
if(n==0 && n==1)
return 1;
int i;
long m=1;
for(i=2;i<=n;i++)
m=m*i;
return m;
}
int main()
{
int n;
printf("Number to calculate for: ");
scanf("%d",&n);
while(n<0)
{
printf("Wrong Input! Enter a number greater than or equal to 0 ");
scanf("%d",&n);
}
long ans=factorial(n);
/* if(n>12)
{
printf("Overflow ");
}
*/
//long can't store correct values above 12!
printf("%d! = %ld",n,ans);
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.