Factorial of Given Number

Factorial of given number is the mathematical problem. Factorial of number means the product or multiplication of the number till the given number. consider we having number N then factorial of number N we calculate as N! => N*(N-1)*(N-2)….

Example:   calculate 6 factorial

6! =6*5*4*3*2*1 =720

That is factorial of the number 6 is 720.

Using this technique we implement the factorial program in c.

We implement factorial program in c in two ways as :

  1. Iterative method
  2. Recursive method
  3. Using pointers.

Using iterative method:

This is first method using which we implement the factorial.

In this technique, we use the iterative loop as for loop to iterate and multiply the number up to N-1.

Write a program to find factorial of number

Factorial

C

1234567891011121314151617181920212223#include<stdio.h>#include<conio.h>void main(){int n,i,f=1;printf(“Enter the number to calculate factorial:\n”);scanf(“%d”,&n);//Factorial logic.for(i=1;i<=n;i++)f=f*i;printf(“Factorial of the number %d is %d “,n,f);getch();}

Output :

Enter the number to calculate factorial:

6      <-

Factorial of the number 6 is 720

In above program, we taken input from user to calculate factorial that is 6.now we calculate the factorial of the number using the formula N! .we taken for loop for calculating this till 6.

Using recursive method

This is second method using which we implement the factorial.

In this technique, we recursively call the same function to calculate factorial.

Recursion: Function calling itself is referred as recursion.

Write a program to find factorial of number using recursion.

C

123456789101112131415161718192021222324252627282930313233343536373839404142434445#include<stdio.h>#include<conio.h>int fact(int); //declaring the fact function.void main(){int n,f;printf(“Enter the number to calculate factorial:\n”);scanf(“%d”,&n);f=fact(n); //calling recursive function with given number n.printf(“Factorial of the number %d is %d “,n,f);getch();}//defining the recursion function.int fact(int n){int fac;if(n<=1){return 1;}else{fac=fac*fact(n-1); //recursive call to the function.return (fac);}}

Output :

Enter the number to calculate factorial:

6      <-

Factorial of the number 6 is 720

Using Pointers method

This is third method using which we implement the factorial.

Pointers : pointers is the variable which stores the address of another variable.

Program to find factorial of number using pointers.

C

123456789101112131415161718192021#include<stdio.h>#include<conio.h>void fact(int,int *); //declaring the fact function with pointer variable.void main(){int n,f,i;printf(“Enter the number to calculate factorial:\n”);scanf(“%d”,&n);fact(n,&f); //calling  function with given number n.printf(“Factorial of the number %d is %d “,n,f);getch();}

Output :

Enter the number to calculate factorial:

6      <-

Factorial of the number 6 is 720

Defining the recursion function.

factorial

C

12345678910111213int fact(int n,int *fac){int i;*fac=1;for(i=1;1<=n;i++){*fac=*fac*i; //calculate factorial using *fac pointer variable.}}

Output :

Enter the number to calculate factorial:

6      <-

Factorial of the number 6 is 720