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

Write a non-recursive function to calculate the n factorial as n!= n × (n-1) × (

ID: 3630946 • Letter: W

Question

Write a non-recursive function to calculate the n factorial as n!= n × (n-1) × (n-2) × … × 2×1. For example, if n=5, then n! = 5×4×3×2×1 =120. Write a program to generate a random number between 1 and 10 and call the function to compute its factorial.

below is the code that someone gave me before .. however my professor said its wrong it prints out only 0.... can anyone please write it in a different way or try to fix it



#include <stdio.h>
#include<math.h>

int fact(int n)
{
int i, f=1;
if(n==0)
return 1;
for(i=1;i<=n;i++) f*=i;
return f;
}

int main()
{
int n;
n=random(10);
printf("%d ", fact(n));
}

Explanation / Answer

please rate - thanks


#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int fact(int n)
{
int i, f=1;
for(i=1;i<=n;i++)
    f*=i;
return f;
}

int main()
{srand(time(0));
int n;
n=rand()%10+1;
printf("%d!=%d ",n, fact(n));
}

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