Slip 29 - A) Write a C program to calculate factorial of a number using user defined function.

Solution:

#include <stdio.h>
int fact(int);
int main()
{    int n;
        printf("Enter an integer: ");
    scanf("%d",&n);
    // show error if the user enters a negative integer
                   printf("Factorial of %d = %llu", n, fact(n));
        return 0;
}
int fact(int n)
{
int i;
unsigned long long factorial = 1;
for(i=1; i<=n; ++i)
        {
            factorial *= i;              // factorial = factorial*i;
           return factorial;
        }
}

Post a Comment

0 Comments