20.09.2015 Views

Programming in C

Kochan - ProgramminginC

Kochan - ProgramminginC

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Exercises<br />

93<br />

Exercises<br />

1. Type <strong>in</strong> and run the 10 programs presented <strong>in</strong> this chapter. Compare the output<br />

produced by each program with the output presented after each program <strong>in</strong> the<br />

text.Try experiment<strong>in</strong>g with each program by key<strong>in</strong>g <strong>in</strong> values other than those<br />

shown.<br />

2. Write a program that asks the user to type <strong>in</strong> two <strong>in</strong>teger values at the term<strong>in</strong>al.<br />

Test these two numbers to determ<strong>in</strong>e if the first is evenly divisible by the second,<br />

and then display an appropriate message at the term<strong>in</strong>al.<br />

3. Write a program that accepts two <strong>in</strong>teger values typed <strong>in</strong> by the user. Display the<br />

result of divid<strong>in</strong>g the first <strong>in</strong>teger by the second, to three-decimal-place accuracy.<br />

Remember to have the program check for division by zero.<br />

4. Write a program that acts as a simple “pr<strong>in</strong>t<strong>in</strong>g” calculator.The program should<br />

allow the user to type <strong>in</strong> expressions of the form<br />

number<br />

operator<br />

The follow<strong>in</strong>g operators should be recognized by the program:<br />

+ - * / S E<br />

The S operator tells the program to set the “accumulator” to the typed-<strong>in</strong> number.<br />

The E operator tells the program that execution is to end.The arithmetic operations<br />

are performed on the contents of the accumulator with the number that was<br />

keyed <strong>in</strong> act<strong>in</strong>g as the second operand.The follow<strong>in</strong>g is a “sample run” show<strong>in</strong>g<br />

how the program should operate:<br />

Beg<strong>in</strong> Calculations<br />

10 S Set Accumulator to 10<br />

= 10.000000 Contents of Accumulator<br />

2 / Divide by 2<br />

= 5.000000 Contents of Accumulator<br />

55 - Subtract 55<br />

-50.000000<br />

100.25 S Set Accumulator to 100.25<br />

= 100.250000<br />

4 * Multiply by 4<br />

= 401.000000<br />

0 E End of program<br />

= 401.000000<br />

End of Calculations.<br />

Make certa<strong>in</strong> that the program detects division by zero and also checks for<br />

unknown operators.<br />

5. You developed Program 5.9 to reverse the digits of an <strong>in</strong>teger typed <strong>in</strong> from the<br />

term<strong>in</strong>al. However, this program does not function well if you type <strong>in</strong> a negative

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!