Tuesday, July 31, 2012

Factorial Computation Using Iteration ( Programming Language C )

 This post will show the source code of factorial computation using iteration. In mathematics, the factorial of a non-negative integer n, denoted n!, is the product of all positive integers less than or equal to n. For example:
 5! = 5 x 4 x 3 x 2 x 1 = 120 

  Iteration means the act of repeating a process usually with the aim of approaching a desired goal or target or result. In this case, for loop is represent the Iteration.
 
Source Code
Code::Blocks

#include <stdio.h>
#include <conio.h>

void main()
{
    int fact (int);
    int number,factnum;

    printf("\nEnter a positive number: ");
    scanf("%d", &number);
    factnum = fact(number);
    printf("\nFactorial number for %d\! = %d", number,factnum);

    getch();
}

int fact (int number)
{
    int i,fact=1;

    for (i=number;i>1;i--)
    {
        fact=fact*i;
    }

    return fact;
}

No comments:

Post a Comment