Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program that determines the largest number for which Hercules can repres

ID: 3624959 • Letter: W

Question

Write a program that determines the largest number for which Hercules can represent its factorial exactly using the float, double, and long double data types. In each iteration, your program should multiply the prior factorial by the next integer. To determine whether you are done, your program should then compare the value of the factorial to the value of the factorial minus one. When the maximum precision of the data type has been exceeded, the least significant digits of the product will be truncated, so subtracting one will not affect the value. That is, the value of the factorial minus the value of the factorial minus one will be the same.

You do not need to write a program that handles all of floats, doubles, and long doubles simultaneously. Write the program so that it handles just one of the three data types and then run it to generate the output for that data type. When you are ready to do the next data type, change the data type in the source code, recompile it, and rerun the program using the new data type.

I’m not fussy about the formatting of the output. However, I want you to output the values of the factorial and the factorial minus one similar to that shown below.

1 1.000000 0.000000
2 2.000000 1.000000
3 6.000000 5.000000
4 24.000000 23.000000
5 120.000000 119.000000
.
.
.

The second last line of the output should be the one for the largest number for which Hercules can represent its factorial exactly. The last line should clearly show that subtracting one from the factorial did not change its value.

When you are ready to run your program, do three runs: one for floats, one for doubles, and one for long doubles. Submit your source code and a script for each data type that captures the following activities: compiling your program and executing your program.

Explanation / Answer

please rate - thanks

#include <iostream>
using namespace std;
int main()
{float nfact=1;
int n;
cout<<"n n! n!-1 ";
do
{cout<<n<<" "<<nfact<<" "<<nfact-1<<endl;
n++;
nfact*=n;
}while(nfact!=nfact-1);
cout<<"Largest float factorial representable is "<<n-1<<"! ";
system("pause");
return 0;
}

----------------------------------------------

#include <iostream>
using namespace std;
int main()
{double nfact=1;
int n;
cout<<"n n! n!-1 ";
do
{cout<<n<<" "<<nfact<<" "<<nfact-1<<endl;
n++;
nfact*=n;
}while(nfact!=nfact-1);
cout<<"Largest double factorial representable is "<<n-1<<"! ";
system("pause");
return 0;
}

------------------------------------------------------

#include <iostream>
using namespace std;
int main()
{long double nfact=1;
int n;
cout<<"n n! n!-1 ";
do
{cout<<n<<" "<<nfact<<" "<<nfact-1<<endl;
n++;
nfact*=n;
}while(nfact!=nfact-1);
cout<<"Largest long double factorial representable is "<<n-1<<"! ";
system("pause");
return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote