Question: Write a C program, which reads a positive integer n from the command line argument, computes and prints the factorials i!, for i from 1

Write a C program, which reads a positive integer n from the command line argument, computes and prints the factorials i!, for i from 1 to n, and n!. Use int type in computing, and use format %11d to print int number and four numbers per line. Your program should be robust for invalid argument input and overflow.

Hint: use for loop to compute i!, assume prev = (i-i)!, then next = i! = i(i-1)! = iprev. If prev == next / i, then next is a correct value, no overflow happens; otherwise overflow happens.

write your program referring to the following.

#include  #include  int main(int argc, char *args[]) { int i, n = 0, f = 1, prev, is_overflow = 0; if ( argc > 1 ) { n = atoi( args[1] ); // convert command line argument to an integer if (n >= 1) { // the conversion is successful // your solution logic code } else { printf("%s:invalid argument ",args[1]); } } else { printf("no argument"); } return 0; }

public test

gcc factorial.c -o factorial factorial 6 1 2 6 24 120 720 6!:720 factorial 15 1 2 6 24 120 720 5040 40320 362880 3628800 39916800 479001600 overflow:13! factorial a a:invalid argument

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!