C Program – Find the factorial of given number

By | 2017-08-20T11:28:55+00:00 August 20th, 2017|c-program|0 Comments

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; 
}

[box type=”success” align=”alignleft” class=”” width=”100%”]

Enter any number : 10
Factorial of 10 is : 3628800

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

[/box]

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)); 
}

[box type=”success” align=”alignleft” class=”” width=”100%”]

Enter any number : 5
Factorial of 5 is : 120

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

[/box]

Happy Learning 🙂

About the Author:

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+ ,

Leave A Comment