Lab Exercise: Computing Factorials in C, Lab Reports of Computer Science

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

Pre 2010

Uploaded on 08/09/2009

koofers-user-3ae-1
koofers-user-3ae-1 🇺🇸

9 documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CPS 196 Lab 10 Fall 2007
Name: ________________________________________________
1. Financial Problem 2 from class.
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%?
pf2

Partial preview of the text

Download Lab Exercise: Computing Factorials in C and more Lab Reports Computer Science in PDF only on Docsity!

CPS 196 Lab 10 Fall 2007

Name: ________________________________________________

  1. Financial Problem 2 from class.

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%?

2. A. In addition to accumulating a sum as we did in a previous lab, sometimes we want to

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

  1. Click OK and when it asks you for this run only or for all programs with the same name, select for all programs with the same name. You will now have a scrollbar on the window, but you

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?