Monday , October 23 2017
Home / c-program / C Program – Find the factorial of given number

C Program – Find the factorial of given number

C Program to find the factorial of given number.

Factorial Of Given Number :


#include<stdio.h> 
int main(void) 
{ 
    int n,num; 
    long fact=1; 
    printf("Enter any number : "); 
    scanf("%d",&n); 
    num=n; 
    if(n<0) 
        printf("Invalid Number(-ve)\n"); 
    else 
    { 
        while(n>1) 
        { 
            fact*=n; 
            n--; 
        } 
        printf("Factorial of %d is : %ld\n",num,fact); 
    } 
    return 0; 
}

Enter any number : 10
Factorial of 10 is : 3628800

Enter any number : -5
Invalid Number(-ve)

Factorial Of Given Number Recursive:


#include<stdio.h> 
long int factorial(int n); 
int main(void) 
{ 
    int num; 
    printf("Enter any number : "); 
    scanf("%d", &num); 
    if(num<0) 
        printf("Invalid Number(-ve)\n"); 
    else 
        printf("Factorial of %d is : %ld\n", num, factorial(num) ); 
 
    return 0; 
} 
 
long int factorial(int n) 
{ 
    if(n==0) 
        return 1; 
    return(n*factorial(n-1)); 
}

Enter any number : 5
Factorial of 5 is : 120

Enter any number : -5
Invalid Number(-ve)

Happy Learning ūüôā

About chandrashekhar

Hi Folks, you have reach this so far, that shows you like what you are learning. Then why don't you support us to improve for bettor tutorials by leaving your valuable comments and why not you keep in touch with us for latest updates on your favorite blog @ facebook , twitter , Or Google+ ,

Recommended

C Program to add elements of an array

How to add elements in an array using C Program. Add elements of an Array …

Leave a Reply

Your email address will not be published. Required fields are marked *