Java Reference
In-Depth Information
2.30 (Separating the Digits in an Integer) Write an application that inputs one number consist-
ing of five digits from the user, separates the number into its individual digits and prints the digits
separated from one another by three spaces each. For example, if the user types in the number 42339 ,
the program should print
4 2 3 3 9
Assume that the user enters the correct number of digits. What happens when you enter a
number with more than five digits? What happens when you enter a number with fewer than five
digits? [ Hint: It's possible to do this exercise with the techniques you learned in this chapter. You'll
need to use both division and remainder operations to “pick off ” each digit.]
2.31 (Table of Squares and Cubes) Using only the programming techniques you learned in this
chapter, write an application that calculates the squares and cubes of the numbers from 0 to 10 and
prints the resulting values in table format, as shown below.
number square cube
0 0 0
1 1 1
2 4 8
3 9 27
4 16 64
5 25 125
6 36 216
7 49 343
8 64 512
9 81 729
10 100 1000
2.32 (Negative, Positive and Zero Values) Write a program that inputs five numbers and deter-
mines and prints the number of negative numbers input, the number of positive numbers input and
the number of zeros input.
Making a Difference
2.33 (Body Mass Index Calculator) We introduced the body mass index (BMI) calculator in
Exercise 1.10. The formulas for calculating BMI are
weightInPounds
×
703
BMI
=
------------------------------------------------------------------------------------
×
heightInInches
heightInInches
or
weightInKi
log
rams
BMI
=
---------------------------------------------------------------------------------------
heightInMeters
×
heightInMeters
Create a BMI calculator that reads the user's weight in pounds and height in inches (or, if you pre-
fer, the user's weight in kilograms and height in meters), then calculates and displays the user's
body mass index. Also, display the following information from the Department of Health and
Human Services/National Institutes of Health so the user can evaluate his/her BMI:
BMI VALUES
Underweight: less than 18.5
Normal: between 18.5 and 24.9
Overweight: between 25 and 29.9
Obese: 30 or greater
 
Search WWH ::




Custom Search