Calculations

C program to find factorial of a number

C program to find factorial of a number

In this tutorial, we will discuss the concept of C program to find factorial of a number

Factorial is a product of all positive descending integer begins with a specified number (n) and calculates up to one

Factorial of number of n

 

Example

factorial of 5 is

5!=5*4*3*2*1=120

 

factorial of 4 is

4!=4*3*2*1=24

 

The factorial of a positive number n is given below

factorial of n is

n!=n*(n-1)*....2*1

 

There are many ways to calculate a factorial using C programming language. some of them are described as below.

the user can provide numbers as they wish and get the factorial according to their input

Code to find factorial of a number

Find factorial using for loop

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int n,i,factorial=1;
    printf("Enter an integer..: ");
    scanf("%d",&n);

    if(n<0){
        printf("Factorial doesn't available to a negative number");
    }

    else{
        for(i=1; i<=n; i++){
            factorial*=i;  //this is equal to factorial=factorial*1

        }
           printf("Factorial of %d is = %d ",n,factorial);
    }
    getch();
    return 0;
}

When the above code is executed, it produces the following results

case 1

Enter an integer..:8
Factorial of 8 is= 40320

 

case 2

Enter an integer..:0
Factorial of 0 is= 1

 

case 3

Enter an integer..:-3
Factorial doesn't available to a negative number

 

Find factorial using the while loop

Program 2

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int n,i,factorial=1;
    printf("Enter an integer..: ");
    scanf("%d",&n);

    if(n<0){
        printf("Factorial doesn't available to a negative number");
    }

    else{
            i=1;
        while(i<=n){
            factorial*=i;  //this is equal to factorial=factorial*1
           i++;
        }
           printf("Factorial of %d is = %d",n,factorial);
    }
    getch();
    return 0;
}

When the above code is executed, it produces the following results

Enter an integer..:5 
Factorial of 5 is= 120

 

Find factorial using the do-while loop

Program 3

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int n,i,factorial=1;
    printf("Enter an integer..: ");
    scanf("%d",&n);

    if(n<0){
        printf("Factorial doesn't available to a negative number");
    }

    else{
            i=1;
    do{
        factorial*=i;  //this is equal to factorial=factorial*1
           i++;

        }while(i<=n);
           printf("Factorial of %d is = %d",n,factorial);
    }
    getch();
    return 0;
}

When the above code is executed, it produces the following results

Enter an integer..:6 
Factorial of 6 is= 720

 

Find factorial using recursion

#include <stdio.h>
#include <stdlib.h>

long factorial(int);  //function declaration
int main()
{
    int n;
    long fact;

    printf("enter the integer to find factorial\n");
    scanf("%d", &n);

    if(n<0)
        printf("Factorial doesn't available to a negative number");
    else{
        fact=factorial(n);//call the method
        printf("%d! = %ld\n",n,fact);
    }
    getch();
    return 0;
}
long factorial(int n)
{

    if(n==0)
        return 1;
    else
        return(n*factorial(n-1));
}

When the above code is executed, it produces the following results

enter the integer to find factorial
6
6!=720

 

Find factorial using the function

#include <stdio.h>
#include <stdlib.h>
long factorial(int);
int main()
{
    int num;
    long fact=1;

    printf("Enter a number to calculate factorial\n");
    scanf("%d",&num);


    printf("%d! = %ld\n",num,factorial(num));
    getch();
    return 0;
}
long factorial(int n)
{
    int i;
    long result=1;
    for(i=1; i<=n; i++)
        result=result*i;
    return result;
}

When the above code is executed, it produces the following results

Enter a number to calculate factorial
6
6!=720

 

Suggested for you

C program operator

C program if statements

C program for loop

C program while loop

C program do-while loop

C program function

C program recursion

Cpp program to find factorial of a number
Python program to find factorial of a number
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Recent Posts

PHP Star Triangle pattern program

PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…

1 month ago

PHP Full Pyramid pattern program

PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…

1 month ago

5 methods to add two numbers in Java

5 methods to add two numbers in Java In this tutorial, we will discuss the…

2 months ago

Python Full Pyramid star pattern program

Python full Pyramid star pattern program In this tutorial, we will discuss  the concept of…

5 months ago

Write a function or method to convert C into F -Entered by user

Write a function or method to convert C into F -Entered by the user In…

9 months ago

How to write a function or method to convert Celsius into Fahrenheit

How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…

10 months ago