English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

3 answers

Recursive Factorial calculation:-

#include

long factorial(long number) {
if(number <= 1)
return 1;
return (number * factorial(number - 1));
}



void main(void) {
long number;

cout << "Please enter a positive integer: ";
cin >> number;
if (number < 0)
cout << "That is not a positive integer.\n";
else
cout << number << " factorial is: " << factorial(number) << endl;
}

2006-11-02 19:25:06 · answer #1 · answered by mashiur1973 2 · 1 0

All the programs above are great, except that they are not taking care of overflow condition. Below program will take care of the overflow conditions. When run in turboc the maz input you can give is 8. After that the program starts overflowing.

#include
#include

unsigned long factorial(int n);
void main()
{
clrscr();
int n;
printf("Enter the number for the factorial ");
scanf("%d",&n);
if(n <= 0)
{
printf("no factorial for -ve numbers");
getch();
return;
}
unsigned long fact = factorial(n);
if((long)fact > 0)
printf("The factorial of %d is %ld\n",n,fact);
else printf("Number too big");
getch();
}

unsigned long factorial(int n)
{
if (n == 1) return 1;
long old_fact = factorial(n-1);
if ((long)old_fact < 0)
{
return -1;
}
else
return (n * old_fact);
}

2006-11-03 04:23:09 · answer #2 · answered by manoj Ransing 3 · 1 0

u want some body to write the program for you,
really amazing.

2006-11-03 03:24:09 · answer #3 · answered by lakshmi r 4 · 0 1

fedest.com, questions and answers