

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Instructions for a lab exercise in cps 196, where students are required to write a program to compute the factorial of a given number using a loop. The exercise includes examples for 10! and 100!, and discusses the limitations of using integers and doubles to store the results.
Typology: Lab Reports
1 / 2
This page cannot be seen from the preview
Don't miss anything!


CPS 196 Lab 10 Fall 2007
Name: ________________________________________________
A. In the space provided, write down these elements of the program development from the class lecture:
Input variables:
Output variables:
Other variables:
(Don’t need to write down example computation)
Program Steps:
B. Open a new item in Visual Studio and write this program. Include the program steps as
comments in the code.
How many years will it take you to save $100,000 if you save $5000 per year and put it into a
savings account with 5%?
accumulate a product. For example 7! = 1 * 2 * 3 * 4 * 5 * 6 * 7. We can compute this in a loop,
by each time multiplying the product by one more number. To start out our process, the product has to be initialized to 1, not zero. Why?
Put this program into Visual Studio and compute 10! Answer :
/factorialtester.c/ #include<stdio.h>
int main(void) { int i, intprod; double doubprod;
i = 1; intprod = 1; doubprod = 1.0; while (i<=100) { intprod = intprodi; //doubprod = doubprodi; printf("i=%4d\tintprod=%15d\tdoubprod=%15.0f\n", i, intprod, doubprod); i=i+1; } return(0); }
Compute 100! Note that the program prints each of the answers along the way. That is, it prints
1!, 2!, 3!, and so on. (You will need to add scroll bars to your console window in order to see the
early results. With your cursor on the blue bar at the top of the console (output) window, right click
and select properties. Click on Layout and change the height in both places to something big, like
have already lost the early lines, so run the program again. This time you will be able to see the
first line of output.)
Answer (only for 100!) as given by the program :
B. I hope you noticed that the answer to 100! is garbage. The problem is that the integer holding
the answer ran out of space. Modify the program that uses both an integer to accumulate the
product and a double to accumulate the product. You should be able to remove the comment from
the line that assigns to doubleprod during the loop.
How high can you go with the integer product?
Do you have the same limit with the double product?