Chapter 4

Program Looping

Exercises

1.

Type in and run the nine programs presented in this chapter.

2.

Write a program to generate and display a table of n and n^2, for integer values of n ranging from 1 to 10.

3.

Write a program that generates a table of triangular numbers for every fifth triangular number using the following formula:

triangularNumber = n (n + 1) / 2

4.

Write a program that generates a table of factorials from 1 to 10.

5.

Properly format the example program given in the textbook.

6.

Substitute the printf() statement in Program 4.3 for a left-justified statement using the - sign. Compare the outputs.

Answer:

Using a left-justified symbol in the printf() statement along with a field width specification means that, as expected, numbers are aligned to the left side of the max field width rather than the right. If your field width specification is 3, single-digit integers will start on the left of the 3 denoted spaces, and fill in to the right as they reach two or three digits in length.

7.

Determine the purpose of using a decimal point before the field width specification in a printf() statement.

Answer:

Using a decimal point in a field width specificer allows you to tell the compiler how many decimal points you want included. For example, %4.2i would tell the compiler you want to print an integer of total length 4, two digits of which should be after the decimal point.

8.

Modify Program 4.5 to allow the user to type in the number of triangular numbers they want to calculate.

9.

Rewrite Program 4.2 through 4.5, replacing all uses of the for statement with equivalent while statements.

10.

What happens if you type a negative number into Program 4.8? Try it and see.

Answer:

Since the modulo operator sometimes returns negative numbers, the program still reverses the numbers correctly but sometimes prepends a minus sign to each digit! A way to fix this would be to detect a minus sign at the beginning, prepend a minus sign to the beginning of the reversed number, then run the while loop on the equivalent positive number as usual.

11.

Write a program that calculates the sum of the digits of an integer. FOr example, the sum of the digits of the number 1234 would be 1 + 2 + 3 + 4, or 10. The program should accept any arbitrary integer typed in by a user.

results matching ""

    No results matching ""