In this programming assignment, you will use your newfound looping knowledge to
ID: 3832144 • Letter: I
Question
In this programming assignment, you will use your newfound looping knowledge to compute a factorial that can fit within an unsigned 16 bit integer. You should use nested loops to try different factorials until you get to one that does not fit, then exit the loop and print a message defining which number became too large.
As you should recall, a factorial is computed like the following:
3! = 3 * 2 * 1
The pseudo code for one way to do this program is shown below. You can use a different method as long as you use nested loops in a way similar to that shown.
num = 10
overflow = false
while (1) {
int16 = 1
for (i = num; i > 0; i--) {
int16 = int16 * i
if (overflow detected) {
overflow = true
break // Break out of inner loop
}
}
if (overflow) {
print "Factorial overflowed 16 bits when number reached ", num
break // Break out of outer loop
}
num = num + 1
}
Explanation / Answer
#include int main() { int n, i; unsigned long long factorial = 1; printf("Enter an integer: "); scanf("%d",&n); // show error if the user enters a negative integer if (n < 0) printf("Error! Factorial of a negative number doesn't exist."); else { for(i=1; iRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.