1. Factorial
Write a program to calculate factorial of a number. Number being entered by user.
#include<stdio.h>
#include<conio.h>
int fact(int);
void main()
{
int n,result;
clrscr();
printf("Enter the number");
scanf("%d",&n);
result=fact(n);
printf("\n the factorial of %d is %d",n,result);
getch();
}
int fact(int x)
{
if(x==1)
{
return (x);
}
else
{
return(x* fact(x-1));
}
}
Output of the program
Enter the number 5
The factorial of 5 is 120
Write a program to calculate factorial of a number. Number being entered by user.
#include<stdio.h>
#include<conio.h>
int fact(int);
void main()
{
int n,result;
clrscr();
printf("Enter the number");
scanf("%d",&n);
result=fact(n);
printf("\n the factorial of %d is %d",n,result);
getch();
}
int fact(int x)
{
if(x==1)
{
return (x);
}
else
{
return(x* fact(x-1));
}
}
Output of the program
Enter the number 5
The factorial of 5 is 120
No comments:
Post a Comment