BestCProgramming
Sunday, November 20, 2016
Factorial Program in C:
#include<stdio.h>
#include<conio.h>
void
main(){
int
i,fact=1,number;
clrscr();
printf(
"Enter a number: "
);
scanf(
"%d"
,&number);
for
(i=1;i<=number;i++){
fact=fact*i;
}
printf(
"Factorial of %d is: %d"
,number,fact);
getch();
}
Output:
Enter a number: 5 Factorial of 5 is: 120
Factorial Program using recursion in C
#include<stdio.h>
#include<conio.h>
long
factorial(
int
n)
{
if
(n == 0)
return
1;
else
return
(n * factorial(n-1));
}
void
main()
{
int
number;
long
fact;
clrscr();
printf(
"Enter a number: "
);
scanf(
"%d"
, &number);
fact = factorial(number);
printf(
"Factorial of %d is %ld\n"
, number, fact);
getch();
}
Output:
Enter a number: 6 Factorial of 5 is: 720
No comments:
Post a Comment
Newer Post
Older Post
Home
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment