In mathematics, the factorial of a positive integer n, denoted by n!, is the product of all positive integers less than or equal to n.
For example, 5 ! = 5 * 4* 3 * 2* 1 = 120
Using While Loop
Method 1:
//C Program to Calculate Factorial of a Number
#include<stdio.h>
#include<conio.h>
void main()
{
int n,fact=1;
clrscr();
printf("Enter any Number:\n\n");
scanf("%d",&n);
while(n>1)
{
fact=fact*n;
n--;
}
printf("\nFactorial is=%d",fact);
getch();
}
#include<stdio.h>
#include<conio.h>
void main()
{
int n,fact=1;
clrscr();
printf("Enter any Number:\n\n");
scanf("%d",&n);
while(n>1)
{
fact=fact*n;
n--;
}
printf("\nFactorial is=%d",fact);
getch();
}
Method 2:
//C Program to Calculate Factorial of a Number
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i=1,f=1;
clrscr();
printf("Enter any Number:\n\n");
scanf("%d",&n);
while (i<=n)
{
f=f*i;
i++;
}
printf("\nFactorial is=%d",f);
getch();
}
Hope you liked my posts, give comments if you have any doubts.. :)#include<stdio.h>
#include<conio.h>
void main()
{
int n,i=1,f=1;
clrscr();
printf("Enter any Number:\n\n");
scanf("%d",&n);
while (i<=n)
{
f=f*i;
i++;
}
printf("\nFactorial is=%d",f);
getch();
}
0 comments:
Post a Comment