Digital जीवन

Free Online Education for India...

Full width home advertisement

Computer Basic

C Programming

Engineering Graphics

Post Page Advertisement [Top]

Dr. A.P.J. Abdul Kalam Technical University, Lucknow

KCS151 / KCS251 Programming for Problem Solving - Using C Language


Lab Exercises


44. Write a program to find the factorial of given number using command line argument.


/*
 File: Prgrm44.c
 Author: Aditya Saini
 Date: Jan 20, 2021
 Description: Program to find the factorial of given number using command line argument.
*/

#include <stdio.h>
#include <stdlib.h>

int main (int argc, char *argv[])
{
     int num, fact;
     int i;

     if (argc == 1)
          printf ("Error! No command line argument found");
     else
     {
          num = atoi (argv[1]);

          //Calculate factorial
          fact = 1;
          for (i = 1; i <= num; i++)
               fact = fact * i;

          //Print result
          printf ("Factorial of %d is %d.", num, fact);
     }

     return 0;
};

Output

C:\Users\digita>"G:\KCS151 or KCS251\Prgrm44.exe" 5
Factorial of 5 is 120



No comments:

Post a Comment

Please do not post spam links.

Bottom Ad [Post Page]

| Designed by Colorlib