C program for factorial | factorial program in c

Hey, guys here is another useful c program for factorial. Today we will learn factorial program in c. The program asks the user to input a number and the program give an output as factorial of a given number based on user input. so let’s start.

what is factorial?

the factorial of a non-negative integer n, denoted by n! it is the product of all positive integers less than or equal to n.

For example,

factorial of 5!  is 120

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

factorial of 6! is 720

6!=6*5*4*3*2*1=720

Note- Factorial of 1 is 1 and factorial of 0 is also 1.

you may also like… python program to find the factorial of a number

 

C program for factorial using for loop

 

#include <stdio.h>
int main()
{
    int n, i,f=1;
    printf("Enter an integer Number: ");
    scanf("%d",&n);
    if (n < 0)
        printf("Factorial of a negative number doesn't exist");
    else
    {
        for(i=1; i<=n; ++i)
        {
            f= f*i;            
        }
        printf("Factorial of %d = %d", n, f);
    }
    return 0;

}

factorial program in c using while loop

 

#include <stdio.h>
int main()
{
    int n, i,f=1;
    printf("Enter an integer Number: ");
    scanf("%d",&n);
    i=n;
    if (n < 0)
        printf("Error! Factorial of a negative number doesn't exist.");
    else
    {
        while(n!=0)
        {
            f= f*n--;            
        }
        printf("Factorial of %d = %d", i, f);
    }
    return 0;

}
factorial program in c C program for factorial
the output of factorial program in c

 

output:-
Enter an integer Number: 4
Factorial of 4 = 24
Enter an integer Number: 5
Factorial of 5 = 120
Enter an integer Number: 0
Factorial of 0 = 1
Enter an integer Number: 1
Factorial of 1 = 1

you may also like…

  • Process scheduling Algorithm
  • C Program for Shortest Job First (SJF) Scheduling Algorithm
  • C/C++ Program for Priority Scheduling Algorithm
  • Selection Sort in C & C++ – Program & Algorithm
  • Bubble sort in c & C++
  • Heap sort in c & c++
  • Merge sort in c & c++
  • Insertion Sort in c & c++

Leave a Reply

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