Hello to all coders out there.
I have a doubt regarding the format "long double" which is producing wierd results when used in the case of a factorial program.
When "long double" used with C lang. produces the output
input = 3
factorial = -2.000000
while when used with C++ gives
input = 3
factorial = 6
So does it imply that long double doesnt work or is not supported by C and only by C++.
Thanks in advance for your help.
The programs in both lang. are given below
// code in C lang
#include <stdio.h>
long double factorial (int input) {
long double result = 1;
while (input) {
result *= input--;
}
return result;
}
int main() {
int input = 1;
long double result = 0;
printf("\nEnter the number whose factorial you want : ");
scanf("%d", &input);
result = factorial(input);
printf("\nThe factorial of %d is %Lf", input, result);
return 0;
}
// In C++ lang.
#include <iostream>
using namespace std;
long double factorial (int input) {
long double result = 1;
while (input) {
result *= input--;
}
return result;
}
int main() {
int input;
long double result = 0;
cout<<"\nyoEnter the number whose factorial you want : ";
cin>>input;
result = factorial(input);
cout<<"\nThe factorial of "<<input<<" is "<<result;
return 0;
}