Monday, 3 August 2015

Calculate Factorial Of a Number

/*
 * C Program to calculate factorial of a number.
 * A factorial of a number n is calculated and represented as follows : 
 * n! = n*(n-1)*(n-2)* ...... 1
 * 0! is considered as 1.
 * factorial of negative number is undefined.
 * Example : 4! = 4*3*2*1 = 24
 *
 * Limitation : Can calculate factorial upto 65 only. 
 * We can increase this number by using unsigned long long, 
 * but it may not be supported on all the compilers.
 */

// Include
#include<stdio.h>

// Declarations
/* factorialSimple
 * Description : Simple factorial function. 
 *               Calculates factorial of a number by using loop.
 * Parameters  : ui_number is the number of which factorial is to be calculated.
 * Return      : returns calculated factorial as an unsigned int.
 */
unsigned long factorialSimple(unsigned long ui_number);

/* factorialRecursive
 * Description : Recursive factorial function. 
 *               Calculates factorial of a number by using recursive function call.
 * Parameters  : ui_number is the number of which factorial is to be calculated.
 * Return      : returns calculated factorial as an unsigned int.
 */
unsigned long factorialRecursive(unsigned long ui_number);

// Definitions
int main()
{
    int i_number;
    unsigned long ui_result;
    printf("Enter number of which factorial needs to be calculated : ");
    scanf("%d",&i_number);
    
    if (i_number >=0 )
    {
        // Let us calculate factorial by using simple method of looping
        ui_result = factorialSimple(i_number);
        printf("Factorial using simple method    : %lu\n",ui_result);
        
        // Now let's try using recursive function call
        ui_result = factorialRecursive(i_number);
        printf("Factorial using recursive method : %lu\n",ui_result);
    }
    else
    {
        printf("Factorial of negative number is undefined.\n");
    }
}

unsigned long factorialSimple(unsigned long ui_number)
{
    unsigned long result = 1;
    
    // If number is 0 or 1 - return 1
    if ((0 == ui_number) || (1 == ui_number))
        return 1;

    // Calculate by multiplying each number starting with ui_number till 2
    for (; ui_number > 1; ui_number--)
        result *= ui_number;
    
    return result;
}

unsigned long factorialRecursive(unsigned long ui_number)
{
    // If number is 0 or 1 - return 1
    if ((0 == ui_number) || (1 == ui_number))
        return 1;
    
    // Calculate factorial by giving recursive call
    return ui_number*factorialRecursive(ui_number-1);

}


Sample Output :
Enter number of which factorial needs to be calculated : 4
Factorial using simple method    : 24
Factorial using recursive method : 24

Enter number of which factorial needs to be calculated : 65
Factorial using simple method    : 9223372036854775808
Factorial using recursive method : 9223372036854775808

Enter number of which factorial needs to be calculated : 0
Factorial using simple method    : 1
Factorial using recursive method : 1

Enter number of which factorial needs to be calculated : 1
Factorial using simple method    : 1
Factorial using recursive method : 1

Enter number of which factorial needs to be calculated : -8
Factorial of negative number is undefined.

No comments:

Post a Comment