Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

CSU07207/ ITU07207 – Procedural Programming

Lab Exercises 3.
1. (Gas Mileage) Drivers are concerned with the mileage obtained by their
automobiles. One driver has kept track of several tankfuls of gasoline by recording
miles driven and gallons used for each tankful. Develop a program that uses scanf
to input the miles driven and gallons used for each tankful. The program should
calculate and display the miles per gallon obtained for each tankful. After
processing all input information, the program should calculate and print the
combined miles per gallon obtained for all tankfuls. Here is a sample input/output
dialog:
Enter the gallons used (-1 to end): 12.8
Enter the miles driven: 287
The miles/gallon for this tank was 22.421875

Enter the gallons used (-1 to end): 10.3


Enter the miles driven: 200
The miles/gallon for this tank was 19.417475

Enter the gallons used (-1 to end): 5


Enter the miles driven: 120
The miles/gallon for this tank was 24.000000

Enter the gallons used (-1 to end): -1

The overall average miles/gallon was 21.601423

2. (How Fast Is Your Computer?) How can you determine how fast your own
computer operates? Write a program with a while loop that counts from 1 to
1,000,000,000, incrementing by 1 during each iteration of the loop. Every time the
count reaches a multiple of 100,000,000, print that number on the screen. Use your
watch to time how long each 100 million iterations of the loop takes. [Hint: Use the
remainder operator to recognize each time the counter reaches a multiple of
100,000,000.]
3. (Tabular Output) Write a program that uses looping to print the following table of
values. Use the tab escape sequence, \t, in the printf statement to separate the
columns with tabs.
N 10*N 100*N 1000*N
1 10 100 1000
2 20 200 2000
3 30 300 3000
4 40 400 4000
5 50 500 5000
6 60 600 6000
7 70 700 7000
8 80 800 8000
9 90 900 9000
10 100 1000 10000

4. (Factorial) The factorial of a non-negative integer n is written n! (pronounced “n


factorial”) and is defined as follows:
n! = n · (n - 1) · (n - 2) · … · 1 (for values of n greater than or equal to 1)
and
n! = 1 (for n = 0).
For example, 5! = 5 · 4 · 3 · 2 · 1, which is 120.
a. Write a program that reads a non-negative integer and computes and prints
its factorial.
b. Write a program that estimates the value of the mathematical constant e by
using the formula:
1 1 1
𝑒 =1+ + + +⋯
1! 2! 3!
c. Write a program that computes the value of ex by using the formula:
𝑥 𝑥2 𝑥3
𝑒𝑥 = 1 + + + +⋯
1! 2! 3!

You might also like