For Advertisements on this blog Contact Us at developgram@gmail.com or +91-9923438816

Simple Programming Tutorials For Beginners

Sunday, December 11, 2016

Factorial

/*
Summary: The factorial of a positive integer n is equal to 1*2*3*...n
*/

#include <stdio.h>
int main()
{
    int n, i;
    unsigned long long factorial = 1;

    printf("Enter an integer: ");
    scanf("%d",&n);

    // show error if the user enters a negative integer
    if (n < 0)
        printf("Error! Factorial of a negative number doesn't exist.");

    else
    {
        for(i=1; i<=n; ++i)
        {
            factorial *= i;              // factorial = factorial*i;
        }
        printf("Factorial of %d = %llu", n, factorial);
    }

    return 0;
}

No comments:

Post a Comment

SEARCH THIS BLOG