Programming in C

(Barry) #1

94 Chapter 6 Making Decisions


number. Find out what happens in such a case and then modify the program so
that negative numbers are correctly handled. For example, if the number –8645 is
typed in, the output of the program should be 5468–.


  1. Write a program that takes an integer keyed in from the terminal and extracts and
    displays each digit of the integer in English. So, if the user types in 932, the pro-
    gram should display
    nine three two


Remember to display “zero” if the user types in just a 0. (Note:This exercise is a
hard one!)


  1. Program 6.10 has several inefficiencies. One inefficiency results from checking
    even numbers. Because it is obvious that any even number greater than 2 cannot
    be prime, the program could simply skip all even numbers as possible primes and
    as possible divisors.The inner forloop is also inefficient because the value of pis
    alwaysdivided by all values of dfrom 2 through p–1.This inefficiency could be
    avoided by adding a test for the value of isPrimein the conditions of the for
    loop. In this manner, the forloop could be set up to continue as long as no divi-
    sor was found and the value of dwas less than p. Modify Program 6.10 to incor-
    porate these two changes.Then run the program to verify its operation. (Note:In
    Chapter 7, you discover even more efficient ways of generating prime numbers.)

Free download pdf