Explore
ProGrad Programs
Placement Prep
TCS Codevita
Webinars
About Us

Edit
Reply




Edit

Factorial program in C, C++ and Java | Program to find the factorial of a number

Published on 09 Mar 2020

Factorial program in C (with and without using command lines), C++ and other languages is discussed here. There are 4 methods to find the factorial of a number. They are:


  • Using tgamma function
  • Using for loop
  • Using recursion
  • Using functions


The solution for all these methods is explained below.


Explanation: Factorial of a non-negative integer n, denoted by n! It is the product of all positive integers less than or equal to n. For example,

factorial of a number


Program to find the factorial of a number using tgamma function


tgamma function is used to calculate factorial of a number. While using this function, you need to include the math.h header file.This function works only till 20!


C
C++

Output
Input- Enter the number:5 Output- Factorial of the number 5 is 120


Program to find the factorial of a number using functions


C
C++
Java
Python 3

Output
Input- Enter the number:5 Output- Factorial of the number 5 is 120


Program to find the factorial of a number using recursion


Factorial using recursion is easier and less complex.


C
C++
Java
Python 3

Output
Input- Enter the number:5 Output- Factorial of the number 5 is 120


Program to find the factorial of a number using for loop and command line arguments


#include<stdio.h>
int main(int a, char *b[])  
{
int x, y, fact = 1, i;
x = atoi(b[1]);   //atoi function is to convert a character to integer
for(i = 1; i <= x; i++)
{
fact = fact * i;
}
printf("%d", fact);
return 0;
}


Recommended Programs








If you have any feedback about this article and want to improve this, please write to enquiry@faceprep.in