Program to find factorial/product of first n natural numbers using for loop

Requirement:

  • 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 * i;
  5. Print product

Program (Without Function)

#include<stdio.h>
int main()
{
printf("***************************************************************");
printf("\n***************************************************************");
printf("\n** WAP to print factorial/product of first n natural numbers **");
printf("\n**               Created by Sheetal Garg                     **");
printf("\n**                Assistant Professor                        **");
printf("\n**                Phone No:9467863365                        **");
printf("\n***************************************************************");
printf("\n***************************************************************\n");
int n,p=1,i;
printf("Enter no upto which you want to multiply\n");
scanf("%d",&n);
for(i=1;i<=n;i++)
p=p*i;
printf("factorial/Product of first %d natural numbers is %d",n,p);
}

Output 1

***************************************************************
***************************************************************
** WAP to print factorial/product of first n natural numbers **
**               Created by Sheetal Garg                     **
**                Assistant Professor                        **
**                Phone No:9467863365                        **
***************************************************************
***************************************************************
Enter no upto which you want to multiply
5
factorial/Product of first 5 natural numbers is 120

Program (Using Function)

#include<stdio.h>
int findfact(int n)
{
    int product=1;
    for(int i=1;i<=n;i++)
        product=product*i;
    return product;
}
int main()
{
printf("***************************************************************");
printf("\n***************************************************************");
printf("\n** WAP to print factorial/product of first n natural numbers **");
printf("\n**               Created by Sheetal Garg                     **");
printf("\n**                Assistant Professor                        **");
printf("\n**                Phone No:9467863365                        **");
printf("\n***************************************************************");
printf("\n***************************************************************\n");
int n;
printf("Enter no upto which you want to multiply\n");
scanf("%d",&n);
printf("factorial/Product of first %d natural numbers is %d",n,findfact(n));
}

Output 1

***************************************************************
***************************************************************
** WAP to print factorial/product of first n natural numbers **
**               Created by Sheetal Garg                     **
**                Assistant Professor                        **
**                Phone No:9467863365                        **
***************************************************************
***************************************************************
Enter no upto which you want to multiply
5
factorial/Product of first 5 natural numbers is 120

Program (Using Recursive Function)

#include<stdio.h>
int findfact(int n)
{

    if(n==1)
    return 1;
    else
    return n * findfact(n-1);
}
int main()
{
printf("***************************************************************");
printf("\n***************************************************************");
printf("\n** WAP to print factorial/product of first n natural numbers **");
printf("\n**               Created by Sheetal Garg                     **");
printf("\n**                Assistant Professor                        **");
printf("\n**                Phone No:9467863365                        **");
printf("\n***************************************************************");
printf("\n***************************************************************\n");
int n;
printf("Enter no upto which you want to multiply\n");
scanf("%d",&n);
printf("factorial/Product of first %d natural numbers is %d",n,findfact(n));
}

Output 1

***************************************************************
***************************************************************
** WAP to print factorial/product of first n natural numbers **
**               Created by Sheetal Garg                     **
**                Assistant Professor                        **
**                Phone No:9467863365                        **
***************************************************************
***************************************************************
Enter no upto which you want to multiply
5
factorial/Product of first 5 natural numbers is 120
error: You can only copy the programs code and output from this website. You are not allowed to copy anything else.