**C program to find factorial of the given number**

Now today, we will learn to calculate the factorial of a given number entered by the user. We solve this, using two methods using if-else and using for loop. So, let’s move forward to our program-

**Example-1 using if-else**

#include <stdio.h> #include<conio.h> int main() { int num, i; long facto = 1; printf(“…Welcome To EduNews.Tech…”); printf("\n\n nEnter a number to calculate its factorial "); scanf("%d", &num); // shows error if the user enters a negative integer if (num < 0) printf("Error! Factorial is not defined for negative numbers."); else { for (i = 1; i <= num; ++i) { facto *= i; } printf("Factorial of %d = %llu", num, facto); } printf("\n\n\n\n\t\t\tThankyou for Joining Us !"); printf("\n\t\t\t!Regards EduNews !"); getch(); }

**Program Output:**

**Example-2 using for loop**

It is the easiest and simplest way to find the factorial of a number.

#include <stdio.h> #include<conio.h> void main() { int i, num , facto = 1; //defining factorial as 1 since least value is 1 clrscr(); printf("\n\t\t\t...Welcome To EduNews.Tech... "); printf ("\n\nEnter a number to calculate its factorial : "); scanf ("%d", &num); if (num<0) //if the input is a negative integer { printf ("Factorial is not defined for negative numbers."); } else { for(i=1;i<= num;i++) //for loop doesn’t gets executed for input 0 as 1>0, therefore fact value remains 1 { facto = facto * i; // keeps on multiplying and storing in the value of factorial till the input integer is reached } printf("Factorial of %d = %d", num, facto); printf("\n\n\n\n\t\t\tThankyou for Joining Us !"); printf("\n\t\t\t!Regards EduNews !"); } getch(); }

**Program Output:**

The number whose factorial is taken as input and stored in a variable is checked whether it is negative or not. If the integer that is entered by the user is negative then a message is displayed as “Factorial is not defined for negative numbers”. The value of factorial is predefined to be 1 as its least value is 1. The for loop is executed for positive integers (except for 0) In the for loop, the value of factorial is multiplied with each integer and stored successively till the input number is reached.

I hope this post helps you to understand Factorial number and its implementation in C programming language.

Keep coding 🙂

Both methods are explained very nicely