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

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; i
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