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

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

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

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

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!


@@coding::1@@


factorial-of-a-number

Program to find the factorial of a number using functions


@@coding::2@@


Program to find the factorial of a number using recursion


Factorial using recursion is easier and less complex.


@@coding::4@@



factorial-of-a-number-1

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


#include<stdio.h>nint main(int a, char *b[])  n{nint x, y, fact = 1, i;nx = atoi(b[1]);   //atoi function is to convert a character to integernfor(i = 1; i <= x; i++)n{nfact = fact * i;n}nprintf("%d", fact);nreturn 0;n}n



Recommended Programs









factorial-of-a-number-1

c