Factorial of a number in C Programming

Factorial of a number in C Programming

Factorial in C programming language is mostly used to demonstrate the application of While Loop, For Loop and Recursion. Actually factorial series has many real-life uses and its use to solve problems of permutation and combinations.

In this article we try to understand what is factorial series. What are the uses of factorial series.And how to write factorial program in C Programming language

In this blog we will understand how to write Factorial Logic using 3 Different methods

  • Factorial Program using While Loop
  • C Program of Factorial using For Loop
  • Recursive Function to calculate Factorial Value

First Understand what is Factorial Series and it’s Uses

What is Factorial Series?

Factorial Series is denoted by exclamatory Symbol (!) like this. Factorial Series is the multiplication of any natural number to all the numbers smaller than it. So the mathematical representation of the factorial series will be like this
n! = n * (n-1) * (n-2) *(n-3)*………..*3*2*1

What is the Practical Application of Factorial Series?

Factorial Series is used in solving many mathematical problems like Permutation and Combination.
This permutation and combination are further used in complex analysis programming like AI and reporting.
Well, you can say the Factorial series is used to solve many other complex series and operations.

Writing Factorial Program using While Loop

While Loop is very important in every programming language. Apart from while loop you need knowledge of variables, print, and scanf statement. If you don’t know what is C Programming or printf and scanf statement. Then you need to go back to books and refer to an online course.

If you are in the US you can watch the C programming course on Amazon Prime or and you can also watch the C Programming series in the UK as well. Amazon Prime C Programming series is best, free(For Amazon Prime Member), and very simple to understand.

If you are not in US and UK then you should subscribe to the course of C Programming at Newtum.

main()
{
int f , i , n; // Declaring Variables
printf("Please Input the Number\n");
scanf("%d",&n);

f = 1; // Initializing Variables
i=1; // Initialing Counter Variable
while(i <= n)
{
f = f * i;
i++;
}
// Displaying the final output
printf("Factorial of Number %d is %d \n\n",n,f);
}

Output:

Please Input the Number

3

Factorial of Number 3 is 6

Detail explanation of this Factorial Program using a While loop is provided at the bottom, in the how-to section of this blog.

Writing Factorial in C Programming using For Loop

For Loop is very important in every programming language. It’s being used to write complex and nested loops. If you don’t understand for loop then you should learn it for our other blog or courses.

If you are in the US you can watch the C programming course on Amazon Prime or and you can also watch the C Programming series in UK as well. Amazon Prime C Programming series is best, free(For Amazon Prime Member), and very simple to understand.

If you are not in US and UK then you should subscribe to the course of C Programming at Newtum.

main()
{
int f , i , n; // Declaring Variables
printf("Please Input the Number\n");
scanf("%d",&n);
f = 1; // Initializing Variables
for(i=1;i<=n;i++)
{
f = f * i;
}
// Displaying the final output
printf("Factorial of Number %d is %d \n\n",n,f);
}

OUTPUT :

Please Input the Number

5

Factorial of Number 6 is 120

Detail explanation of this Factorial Program using For loop is provided at the bottom, in the how-to section of this blog.

Writing Factorial in C Programming using Recursive Function

The function is a wast topic and it’s not in the scope of this blog. You can always refer our course for C Programming if you want to understand functions in detail

main()
{
int f , i , n; // Declaring Variables
printf("Please Input the Number\n");
scanf("%d",&n);
f = 1; // Initializing Variables
f=factorial(n);
// Displaying the final output
printf("Factorial of Number Using Recursion %d is %d \n\n",n,f);
}
int factorial(int x)
{
int temp;
if(x > 1)
{
temp = factorial(x-1);
return x *temp;
}
else{
return 1;
}
}

OUTPUT

Please Input the Number

6

Factorial of Number 6 is 720

How to Section explains each program of the above C Programs in details

Time needed: 10 hours.

How to write Factorial C Program in 3 Different Ways

  1. Using While Loop

    1 . Declare 3 Variables “n” for input number , “i” for counter and “f” for storing the final results.
    2. Input the number using scanf statements and above that write a printf statement so that user will understand .
    3. Initialize the variable i and f to 1 , it’s necessary because factorial involves multiplication and the value cannot be zero.
    4. Write while loop with condition i <= n , so that our loop will run n times
    5. Inside while loop write multiplication logic f = n* i , so that every time value of i is incremented multiplication will be done.
    6. Increase the value of i every time loop is executed using increment operator
    7. After the while loop print the value of “f” variable.

  2. Using For Loop

    1 . First we will declare 3 Variables “n” for input number , “i” for counter and “f” for storing the final results.
    2. We will ask the user to input the number with the help of printf statement and scanf statement.
    3. Initialize “f” variable with value 1
    3. Write for loop , with i = 1 and condition i < = n
    4. Inside for loop write multiplication logic f = n* i , so that every time value of i is incremented multiplication will be done.
    5. After the for loop print the value of “f” variable.

  3. Factorial Program in C using recursive function

    1 . First we will declare 3 Variables “n” for input number, “i” for counter, and “f” for storing the final results.
    2. We will ask the user to input the number with the help of printf statement and scanf statement.
    3. Initialize “f” variable with value 1
    3. Call the function factorial while passing value as “n”
    4. After the for loop print the value of “f” variable.
    5. Declare the function factorial in the function, accepting input in the variable “x”
    6. Inside the function factorial declare a variable temp.
    7. Write the if and else statement for x > 1 and inside the condition write the required expression as shown in the code and return the value. Here we are calling the same function again till the condition is met. This is a recursion.
    8. In the else condition write return 1.