C Program to Find Total of Even Integers

C Program to Find Factorial of Number Using Recursion

To Write C program that would find factorial of number using Recursion. The function is a group of statements that together perform a task. Every C program has at least one function, which is main(), and all the most trivial programs can define additional functions.

You can divide up your code into separate functions. How you divide up your code among different functions is up to you, but logically the division is such that each function performs a specific task.

A function declaration tells the compiler about a function’s name, return type, and parameters. A function definition provides the actual body of the function.

The C standard library provides numerous built-in functions that your program can call. For example, strcat() to concatenate two strings, memcpy() to copy one memory location to another location, and many more functions.


A function can also be referred as a method or a sub-routine or a procedure, etc.

Steps to find factorial of number using Recursion

To Define a Function

The general form of a function definition in C programming language is as follows:-

return_type function_name( parameter list ) {
body of the function
}

A function definition in C programming consists of a function header and a function body.

Here are all the parts of function −

Return Type − A function may return a value. The return_type is the data type of the value the function returns. Some functions perform the desired operations without returning a value. In this case, the return_type is the keyword void.

Function Name − This is the actual name of the function. The function name and the parameter list together constitutes the function signature.

Parameters − A parameter is like a placeholder. When a function is invoked, you pass a value to the parameter. This value is referred to as actual parameter or argument. The parameters in function definition that receive these argument values are known as formal parameters. The parameter list refers to the type, order, and number of the parameters of a function. Parameters are optional; that is, a function may contain no parameters.

Function Body − The function body contains a collection of statements that define what the function does.

Function Declarations

A function declaration tells the compiler about a function name and how to call the function. The actual body of the function can be defined separately.

A function declaration has the following parts:-

return_type function_name( parameter list );

Calling a Function

While creating a C function, you give a definition of what the function has to do. To use a function, you will have to call that function to perform the defined task.

To call a function, you simply need to pass the required parameters along with the function name, and if the function returns a value, then you can store the returned value.

Recursion is the process of repeating items in a self-similar way. In programming languages, if a program allows you to call a function inside the same function, then it is called a recursive call of the function.

void recursion() {
recursion(); /* function calls itself */
}
int main() {
recursion();
}

The C programming language supports recursion, i.e., a function to call itself. But while using recursion, programmers need to be careful to define an exit condition from the function, otherwise it will go into an infinite loop.

Recursive functions are very useful to solve many mathematical problems, such as calculating the factorial of a number, generating Fibonacci series, etc.

Example : C Program to Find Factorial of Number Using Recursion

Number Factorial

The following example calculates the factorial of a given number using a recursive function

#include <stdio.h>
int factorial(unsigned int i) {
if(i <= 1) {
return 1;
}
return i * factorial(i - 1);
}

int main() {
int i = 15;
printf("Factorial of %d is %d\n", i, factorial(i));
return 0;
}

Output

Factorial of 15 is 2004310016

Save program in a file, Compile program, debug errors, Execute or Run program with necessary inputs. Verify the outputs obtained.

Find Factorial of Number

/*program to find factorial of a given number*/
#include<stdio.h>
#include<math.h>
void main()
{
int n;
long int f;
long int fact(int);
clrscr();
printf("enter a number");
scanf(“%d”,&n);
f=fact(n);
printf("\nfactorial of a given no is: %d ",f);
getch();
}
long int fact(int n)
{
int i;
long int f=1;
for(i=1;i<=n;i++)
{
f=f*i;
}
return f;
}

Output

Enter a number 5
Factorial of a given no is: 120

Let me know if you find any difficulty in understanding this C Program to Find Factorial of Number Using Recursion with example and I would be glad to explain it further.

Online Training Tutorials