# TCS Coding Practice Question | Factorial of a Number

Given a number, the task is to find the Factorial of this number using Command Line Arguments. Factorial of a non-negative integer is the multiplication of all integers smaller than or equal to n. Examples:

```Input: 3
Output: 6
1 * 2 * 3 = 6

Input: 6
Output: 720
1 * 2 * 3 * 4 * 5 * 6 = 720
```

Approach:

• Since the number is entered as Command line Argument, there is no need for a dedicated input line
• Extract the input number from the command line argument
• This extracted number will be in String type.
• Convert this number into integer type and store it in a variable, say num
• Find the reverse of this number and store in a variable, say rev_num
• Check if this rev_num and num are same or not.
• If they are not same, the number is not Palindrome
• If they are same, the number is a Palindrome

Program:

## C

 `// C program to find factorial of a number ` `// using command line arguments ` ` `  `#include ` `#include /* atoi */ ` ` `  `// Function to find factorial of given number ` `unsigned ``int` `factorial(unsigned ``int` `n) ` `{ ` `    ``int` `res = 1, i; ` `    ``for` `(i = 2; i <= n; i++) ` `        ``res *= i; ` `    ``return` `res; ` `} ` ` `  `// Driver code ` `int` `main(``int` `argc, ``char``* argv[]) ` `{ ` ` `  `    ``int` `num, res = 0; ` ` `  `    ``// Check if the length of args array is 1 ` `    ``if` `(argc == 1) ` `        ``printf``(``"No command line arguments found.\n"``); ` ` `  `    ``else` `{ ` ` `  `        ``// Get the command line argument and ` `        ``// Convert it from string type to integer type ` `        ``// using function "atoi( argument)" ` `        ``num = ``atoi``(argv); ` ` `  `        ``// Find the factorial ` `        ``printf``(``"%d\n"``, factorial(num)); ` `    ``} ` `    ``return` `0; ` `} `

## Java

 `// Java program to find the factorial of a number ` `// using command line arguments ` ` `  `class` `GFG { ` ` `  `    ``// Method to find factorial of given number ` `    ``static` `int` `factorial(``int` `n) ` `    ``{ ` `        ``int` `res = ``1``, i; ` `        ``for` `(i = ``2``; i <= n; i++) ` `            ``res *= i; ` `        ``return` `res; ` `    ``} ` ` `  `    ``// Driver code ` `    ``public` `static` `void` `main(String[] args) ` `    ``{ ` ` `  `        ``// Check if length of args array is ` `        ``// greater than 0 ` `        ``if` `(args.length > ``0``) { ` ` `  `            ``// Get the command line argument and ` `            ``// Convert it from string type to integer type ` `            ``int` `num = Integer.parseInt(args[``0``]); ` ` `  `            ``// Get the command line argument ` `            ``// and find the factorial ` `            ``System.out.println(factorial(num)); ` `        ``} ` `        ``else` `            ``System.out.println(``"No command line "` `                               ``+ ``"arguments found."``); ` `    ``} ` `} `

Output:

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :
Practice Tags :

2

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.