Write a C program to do the following: a- read in an input integer from user and
ID: 3862537 • Letter: W
Question
Write a C program to do the following: a- read in an input integer from user and compute the factorial of the input Fact(n)= n*(n-1)*(n-2)*...*3*2*1 Test your code and provide a snapshot of a working input such as n=6, and invalid input such as n= - 3, and n=A 1 Provide the code as part of your work paper b- Write a code that would generate 5 rand numbers between (1-12) in a loop and display a table with factorial values of the random numbers. The code should prompt user for an input of rows of table to display. Possible range of rows (1-5). The table example above was shown for an input of 3 rows supplied by user. Example of a run for the case of 3 rows Test your code and provide a snapshot of a working input such as rows R=3, and invalid input such as R= - 3, R=2.3, and R=A1 Provide the code as part of your work paperExplanation / Answer
//To calculate factorial of a number.
#include <stdio.h>
int main()
{
int n, i;
unsigned long long factorial = 1;
printf("Enter an integer: ");
scanf("%d",&n); //Taking input and storing in 'n'
// If user enters a negative integer
if (n < 0)
printf("Error! Invalid Input. ");
else
{
for(i=1; i<=n; i++)
{
factorial *= i; // factorial = factorial*i;
}
printf("Factorial of %d = %llu ", n, factorial);
}
return 0;
}
//To generate and print factorial of those number.
#include <stdio.h>
#include <stdlib.h>
int main() {
int c, n,i;
unsigned long long factorial = 1;
printf("5 random numbers in [1,12] ");
for (c = 1; c <= 5; c++) {
n = rand() % 12 + 1;
//printf("%d ", n);
for(i=1; i<=n; i++)
{
factorial *= i; // factorial = factorial*i;
}
printf("%d %llu ", n, factorial);
factorial =1;
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.