Factorial program in C Language

Spread the love
factorial-program-in-c-language

Program Description:

In this article, We are going to look at the Factorial program in C language. We are going to use the C Loops like for loop , while loop to calculate the prime number. The program should accept a positive number from the user and calculate the factorials of the number.

What is Factorial?

The factorial of positive integer n is denoted by n!

The Factorial is the product of all positive integers less than or equal to n. ( product of all numbers from 1 to n )

For example, a Factorial of a number 5 is

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

📢 The Factorial of Zero (0) is equal to 1

Factorial Program Example Input and Output:

Input:

Enter a Positive Number : 6

Output:

Factorial of 6 is : 720

The program should also handle invalid input cases.

Pre-Requisites:

It is recommended to know the basics of the C Loops and Goto statement. Please go through the following articles to understand more about them

Factorial Program in C Algorithm:

  1. Take the input number from the user. And we store it in a variable named n
  2. Check if the n is positive or negative. As there is no factorial for negative numbers, Display the error message ( No Factorial for Negative Numbers, Please try again) if the user enters a negative number. Also, allow the user to input the value again.
    • We use the Goto Statement to jump back to the start of the program. To achieve this, We use the goto START; ( at line 20) and START; (at line 11) Label.
  3. Now the n is a positive number. If the number n is equal to Zero(0), Then the factorial of zero(0) is 1. Display the result and return.
    • or you can even remove this check, by default we use the fact variable value as 1, and i value as 1. So if the user enters to check factorial, The loop won’t execute as condition i<=n will be False, So we simply print the default fact number which is 1. )
  4. If the number is any other positive number. We use the Loop to calculate the Factorial of the number.
  5. The main Idea is to Multiply all numbers from 1 to n by iterating from numbers 1 to n
  6. The Loop starts with the number 1 (We can start from 2) and goes all the way up to the number n
    • At Each Iteration, We multiply the fact variable(which is initialized with 1) with the present loop control variable i. fact = fact * i;
  7. The above loop will continue until the loop condition i < = n becomes False.
  8. Once the loop is terminated, The fact variable contains the Factorial of the given number n

Let’s convert the above Pseudo code into the C program.

Factorial program in C language with for loop:

Let’s use the for loop to calculate the factorial of the number

We can even change the above program by removing the if (n == 0) check like below, As the default value of the fact variable is 1, So if the user provides , the for loop won’t execute and we display the default value i.e 1.. Which also gives the same result.

Program Output:

Let’s compile and run the program using the IDE. We are using the GCC compiler. You can learn more about the compile and run the program in Linux in the following articles

Compile the program

$ gcc factorial.c

Run the program:

As we can see from the above output, We are getting the desired results and our program is able to calculate the Factorial of the given number.

What if the user enters a negative number? Let’s try to give the negative number to the program

We can see that, The program displayed an error notice ( No Factorial for Negative Numbers, Please try again) when the user provided a negative number and asked for the number again. This process continues until user enters a positive number.

factorial-program-output-in-c-language

Factorial Program in C using the while loop:

We can also use the while loop to calculate the factorial of a number. The program algorithm is not going to change, Only change is we are going to use the while loop instead of the for loop

Program Output:

As you can see from the above output, We are getting the desired output.

Factorial Program using do while loop:

Let’s change the program to use the do while loop instead of other loops.

Program Output:

Our program is working as expected.

factorial-program-in-c-using-do-while-loop

Practice Program:

  • Try to write the above program without using the loops

📢 Hint: Use the Goto statement to repeat the code block

Learn more about Factorial:

Venkat

Hi Guys, I am Venkatesh. I am a programmer and an Open Source enthusiast. I write about programming and technology on this blog.

You may also like...

2 Responses

  1. […] C Program to Calculate the Factorial of Number […]

  2. […] Related Program: Calculate Factorial Program using iterative method ( Loops ) […]

Leave a Reply