Chapter 4 Programming Exercises PDF

Title Chapter 4 Programming Exercises
Course Comp
Institution Thompson Rivers University
Pages 2
File Size 147 KB
File Type PDF
Total Downloads 53
Total Views 147

Summary

problems and solutions...


Description

CHAPTER4PROGRAMMI NG EXERCI SES CONDITIONAL STATEMENTS Problem 1 Write the code to calculate the BMI (Body Mass Index) and print the appropriate message as described below.

Problem 2 - sort 3 numbers Ask the user for 3 integers, and store these in the variables low, medium, high. Then write some Java code to “sort” them. That is, if low = 50, medium = 10 and high = 30 before your code is executed, then after your code the variables will contain low = 10, medium = 30 and high = 50. Print out the values of low, medium and high before and after they are sorted. The “sort” code needs only 3 separate if statements (with no else). The first two if statements should ensure that high has the correct value. The final if statement will ensure low and medium are correct. Test your code with these 3 different data sets, writing out the values for low, medium and high before and after “sorting” in each case: low = 50, medium = 10, high = 30 low = 30, medium = 50, high = 10 low = 50, medium = 30, high = 10 LOOPS Problem 1 Write a C program to check the math formula for 1 + 2 + … + n = n(n+1)/2. Check it for n=100, and n=10000. Also find the sum of the even numbers between 1 and n, and the odd numbers between 1 and n, then check that sum_of_evens + sum_of_odds = sum_between_1_and_n. Problem 2 Add a loop to the “sort 3” problem so the user can run it several times to test the 3 data sets.

Problem 3 An object is dropped from the top of the Empire State Building (assume the object does not have any air resistance) a distance of 381 m or 443 m to the tip. Use the tip for this problem. Write a Java program that will output a table using the EXACT format below (plus or minus a few spaces) showing the time in seconds and the height above ground at that time, until the object hits the ground. What is the approximate time the object hits the ground? The formula for the distance an object falls due to gravity is d = 0.5gt2, where g = 9.81 m/sec2. Time (seconds) 1 2 3 4 5

Height (meters) 443.0 438.1 423.4 398.9 etc...


Similar Free PDFs