Become Our Fan on Social Sites!

Facebook Twitter

Google+ RSS YouTube

Wednesday, 25 September 2013

Factorial program in c language

Factorial Number: For example Factorial of 5 gives 120 as output, that means 5! =  5*4*3*2*1 (120).



1) Factorial program in c language using for loop.

#include <stdio.h>

int main()
{
    int count, no, fact = 1;

    printf("Enter No. : ");
    scanf("%d", &no);

    for (count = 1; count <= no; count++)
        fact = fact * count;

    printf("Factorial of %d = %d", no, fact);

    return 0;
}


2) Factorial program in c language using recursion.

#include<stdio.h>

long factorial(int);

int main()
{
    long fact;
     int no;

    printf("Enter No : ");
    scanf("%d", &no);

    if (no < 0)
        printf("Negative No Not Allowed. Give Non Negative No");
    else
    {
        fact = factorial(no);
        printf("%d! = %ld", no, fact);
    }
     return 0;
}

long factorial(int no)
{
    if (no == 0)
        return 1;
    else
        return( no * factorial( no-1 ) );
}

0 comments :

Post a Comment