5. Print the all the integers from 1 to 100, their squares, and their square roots.
|
| Csc 123 - Computer Science I, Programming in Java |
|
Practice Problems
-- Repetition
Algorithms using simple counter controlled loops 1. Print all the integers from 1 to 64. 2. Print all the even integers from -64 to 64, inclusive. Make sure you include 0 properly. 3. Print all the integers from 100 to 1, that is, in descending order. 4. Print the entire lyrics to the song "99 Bottles of Beer on the Wall". 5. Print the all the integers from 1 to 100, their squares, and their square roots. 6. Print the all the sines and cosines for the angles from 0 to 90 degrees (hint, use the Math.toRadians function to convert from degrees to radians.) 7. Prompt the user to enter a positive number, then input that number. Print all the integers from 0 up to and including that number.
Running total loops, counter controlled 8. Print the sum of all the integers from 1 to 100. 9. Prompt the user for a positive number and get it from the user, then print the total of all the integers from 1 to that number. 10. Prompt the user for a positive number. Print each integer from to the user's number and the sum of all the integers from 1 up to and including that integer. Your output would start like this:
1 1
Simple event controlled loops 11. Input numbers from the user and print their square roots. Continue until the user enters a number less than zero. 12. Prompt the user for a price, input it. Then calculate the tax, based on a 7.5% rate. Print "Tax" followed by the tax amount, then print "Total", followed by the price plus the tax amount. Next prompt the user for the amount paid, and input it. Echo the amount paid back out, followed by "Change" and the change due the customer. Do all this repeatedly until the user enters 0.00 for the price.
Running total loops, event controlled 13. Input numbers from the user until the user enters a 0. Print the total of the numbers. 14. Input temperatures from the user under the user enters a temperature less than -300. Output the root-mean-square of the temperatures. The root-mean-square is the square root of the average of the squares of all the temperatures.
Algorithms requiring nested structures:
Protective enclosing if's, usually used to check for input errors: 15. Input a number from the user. If it is positive, print all the integers from 0 to that number. Otherwise say that there are no positive integers between 0 and the number.
16. Input a number from the user.
If it is positive, print all the powers of that number from number0
to number12. If it is negative, print
all the powers of that number from number -0
to number
-12.
17. Input numbers from the user until the user enters 0. Print only the positive numbers to the screen. 18. Keep inputting the gross weight of tractor-trailer trucks until the user enters "quit". After the user inputs a weight, ask for the number of axels. Make sure the axels are between 1 and 7, inclusive, and print an error message if they are not. If the weight is over 80,000 lbs, or over 12,000 per axel, issue a "Warning: Overweight truck". Otherwise print "Good to go!" on the screen.
Algorithms that test and store 19. Input 10 numbers from the user and print out the highest. 20. Keep inputting integers from the user until the user enters a 0. Then print out how many of the numbers were even and how many were odd. 21. Ask the user how many test grades need processing, then ask the user for each test grade. Output the highest grade entered, the lowest grade, and the average of all the grades. 22. Ask the user how many test grades need processing, then ask the user for each numeric test grade. After all grades are entered, print how many A's, B's, C's, D's, and F's there were. An A is from 94 to 100 inclusive, a B from 85 to 93 inclusive, a C from 75 to 84, a D from 65 to 74, and an F is 64 or below.
23. Print every possible throw of two dice, along with the total. Assuming one die is colored red and the other blue, your print out should look like this:
Red Blue Total 24. Print out each inch for a yard stick, using a feet and inches format. Be very careful about the limits on the control variables. Your output should look like this.
0' 0"
Some pseudo-thinkers: 31. Keep inputting numbers from the user as long as each number is greater than the previous number. In other words, if the user enters a number that is equal to or smaller than the previous number, stop. 32. Keep inputting numbers from the user until the user enters a 0. After each number, print a "running average". That is, print the average of the last three numbers entered. 33. Input two numbers from the user, who can enter them in any order. If the first is less than the second, print out all the integers from the first to the second in ascending order. Otherwise print all the integers from the first to the second in descending order. Note: there is more than one good solution. 34. Keep inputting integers from the user until the user enters a 0. Then print the highest and lowest number entered, plus the lowest positive number and the highest negative number. If the user does not enter any positive numbers, print 0 as the lowest positive. If the user does not enter any negative numbers, print 0 as the highest negative.
|
| Back to Csc 123 - Computer Science I, Programming in Java |
| Scott Badman Office: B132 Phone: 353-2250 sbadman@parkland.edu |
Parkland College, 2400 W. Bradley Avenue, Champaign, IL 61821 |