Program to print factorial/product of first n natural numbers using Function

Logic:

  • Enter the number n
  • Product or Factorial= 1 x 2x 3 x 4 x………x n

Algorithm

  1. Enter the number as n
  2. Initially assign product=1
  3. for(i=1;i<=n;i++)
  4. product= product

Time Complexity: O(n)

Space Complexity: O(1)

Program

#include <stdio.h>
int fact(int n);
int main()
{
    printf("****************************************************************\n");
    printf("****************************************************************\n");
    printf("**     WAP to print factorial of a number n using function    **\n");
    printf("**               Created by Sheetal Garg                      **\n");
    printf("**                 Assistant Professor                        **\n");
    printf("**                 Phone No:9467863365                        **\n");
    printf("****************************************************************\n");
    printf("****************************************************************\n");
    int n, f;
    printf("\nEnter number to find factorial\n");
    scanf("%d", &n);
    f = fact(n);
    printf("\nFactorial is %d", f);
}
int fact(int n)
{
    int i, f = 1;
    for (i = 1; i <= n; i++)
        f= f * i;
    return f;
}

Output 1

****************************************************************
****************************************************************
**     WAP to print factorial of a number n using function    **
**               Created by Sheetal Garg                      **
**                 Assistant Professor                        **
**                 Phone No:9467863365                        **
****************************************************************
****************************************************************

Enter number to find factorial
5

Factorial is 120
error: You can only copy the programs code and output from this website. You are not allowed to copy anything else.