Skip to main content

C Program to Calculate the Factorial of a number using Function

 C Program to Calculate the Factorial of a number using Function.

    In this example, we will calculate the factorial of a number using function. Here we have used functions with Arguments and return value to perform this operation. We have declared a user-defined function fact which is called in the main function when printing the result.


What is a Factorial of  a Number?
    
The factorial of a number is the product of all positive integers less than, equal to n. Factorial is denoted by the symbol '!'. Formula of factorial is n!=n x (n-1)!

For example:

6!= 6 x 5 x 4 x 3 x 2 x1

   = 720


PROGRAM TO CALCULATE THE FACTORIAL OF A NUMBER USING FUNCTION

#include<stdio.h>
#include<conio.h>
int fact(int);
void main()
{
    int n;
    printf("Enter the number : ");
    scanf("%d",&n);
    printf("Factorial of %d is : %d",n,fact(n));
    getch();
}
int fact(int b)
{
    int i,f=1;
    for(i=1;i<=b;i++)
    {
        f=f*i;
    }
    return(f);
}


OUTPUT OF FACTORIAL OF A NUMBER USING FUNCTION.

  Enter the number : 6

  Factorial of 6 is : 720



NOTE: You Can Comment Your Code if you have solved differently and we will pin it in the comment section.  Let's Learn together. 


MOTTO: 

You Learn, I Learn together We Learn.

Comments

Popular posts from this blog

C Program to Print first N Even Natural Number/in Reverse Order

  C Program to Print first N Even Natural Number.     In this example, we will find and print the first N even natural. Here we have used for loop to solve this program.      What are Natural Numbers? Natural Numbers are all positive numbers starting from 1 to infinity.      For Example:  1,2,3,4,5,6,.............................,n are all natural numbers. What are Even Numbers? Even numbers are those numbers whose remainder is 0 when divided by 2.     For Example: 2,4,6,8,10,.....,n are all even numbers.   PROGRAM: #include <stdio.h> #include <conio.h> void   main () {      int   i , x ;      printf ( "Enter the value of N : " );      scanf ( " %d " ,& x );      for ( i = 1 ; i <= x ; i ++)     {          printf ( " %d " , 2 * i );     }      getch (); } OUTPUT: C Program to Print First N Even Natural Number in Reverse Order. PROGRAM 123 #include <conio.h> #include <stdio.h> int   main () {      int   i , x ;      printf