Factorial program in C using function

Spread the love
factorial-program-in-c-using-function

Program Description:

We are going to look at the Factorial program in C using function in this article, The program will accept an Integer number from the user and display the factorial of a given number. The program shouldn’t accept negative numbers.

Factorial of a Number:

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  4 is

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

Example Input and Output:

Example 1: Input:

Enter a Number: 9

Output:

Factorial of 9 number is : 362880

Example 2: Input:

Enter a Number: 6

Output:

Factorial of 6 number is : 720

Pre-Requisites:

It is recommended to know the basics of the C functions and loops. Please go through the following articles to familiarize yourself with the above concepts

Factorial program in C using function Algorithm:

  1. Take a integer number from the user and store it in variable num1.
  2. We won’t allow the negative numbers for factorial, So display an error message to the user and prompt for the input again.
  3. Create a function named factorial. The factorial function takes one integer number as input and calculates the factorial of a given number. Here is the prototype of the factorial function.
    • int factorial(int);
    • function_name: factorial
    • argument_list : int
    • return_type : int
  4. Call the factorial function by passing the num1. As the factorial function returns an integer variable, collect the return value using the variable result.
  5. The factorial function uses a Loop to calculate the factorial.
    • Create two variables i and fact. Initialize the fact variable with 1.
    • The loop starts from 1 and goes all the way up to the given number num1 ( We use i to control the loop)
    • At each iteration, We multiply the fact variable with loop counter i. – i.e fact *= i;
    • Once all iterations are completed, The fact variable contains the factorial value of num1. So return the fact variable.
  6. Display the result on console.

Factorial program in C using function

Let’s convert above algorithm in to the C program.

Program Output – Factorial program in C using function:

Let’s compile the program using the GCC compiler.

gcc factorial-func.c

The above command generates the executable file called a.out. Run the executable file.

Test Case 1: Postive Numbers:

The program is calculating the factorial value correctly.

Factorial-program-in-C-using-function-output

Test Case 2: Negative numbers:

If the user enters a negative number, As expected, The program is displaying the error message ( Error: Please enter a positive number) and prompting for the input again. The program will prompt the user until user provide a valid number.

We used the Goto statement to prompt the user for input until a valid number is entered.

Related Program:

  1. All C Program List [Index]
  2. C Program to Print Numbers in Reverse Order
  3. C Program to Calculate Sum of First N natural numbers
  4. C Program to Reverse a Number
  5. C Program to Calculate Sum of Digits of a Number
  6. C Program to Calculate Product of Digits of a Number
  7. C Program to Count the number of Digits of a Number
  8. C Program to Multiply without using Multiplication operator (*)
  9. C Program to generate Fibonacci Series up to a given number
  10. C Program to Print First n Fibonacci numbers
  11. C Program to get Nth Fibonacci number
  12. C Program to Check number is Prime Number or not?

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. […] Factorial of a Number using functions […]

  2. […] Factorial of a Number using functions […]

Leave a Reply