Exercise 4 - For Loops
Create a project called Assignment4 and place all questions inside of this project.
Question 1
Redo Question 1 from exercise 3, the count down question, but instead of using a while loop, use a for loop. Name your program Question1
Example Output
10
9
8
7
6
5
4
3
2
1
0
Blast Off!
Question 2
Write a program that will ask the user for a message and the number of times they want that message displayed. Then output the message that number of times. Name your program Question2
Example Output
What message would you like displayed? I Like Cake
How many times would you like to display your message? 4
I Like Cake
I Like Cake
I Like Cake
I Like Cake
Question 3
Create a program that will ask the user for a number and then print out a list of number from 1 to the number entered and the square of the number. Name your program Question3
Example Output
What number would you like to go up to? 5
1 squared is 1
2 squared is 4
3 squared is 9
4 squared is 16
5 squared is 25
Question 4
Write a program to output a backwards count by 5s from 100 down a number between 100 and 50 that is input before the countdown begins. The program should stop when the count would be greater than the number input. Name your program Question4
Example Output
Enter a number between 50 and 100 to stop at: 82
100
95
90
85
Question 5
When $1000 is deposited at 5 percent simple interest, the amount grows by $50 each year (i.e. the interest is calculated on the original principal of $1000 only every time). When money is invested at 5 percent compound interest, then the amount at the end of the year is 1.05 times the amount at the beginning of that year. Write a program to display the amounts for 10 years for a $1000 deposit at 5 percent simple and compound interest. The first few lines displayed in the picture box should appear as shown below. Save your program as Question5.
Amount Amount
Simple Compound
Year Interest Interest
1 $1,050.00 $1,050.00
2 $1,100.00 $1,102.50
3 $1,150.00 $1,157.63
Question 6
Rewrite problem 5 so that the user can enter their own starting principal, their own interest rate and the number of years that they want displayed on the screen. At the end display a grand total for each column. Save this program as Question6.
Question 7
Write a program that will request a number in the range 1 to 20 and display a row of that many stars (asterisks). Hint: when you print, you can tell Java not to put any spaces between this line, and the next line by using the print() command instead of println(). For instance,
System.out.print("Hello ");
System.out.println("World");
will output to the screen "Hello World" all on one line.
Name your program Question7
Example Output
Please enter the number of stars you would like(between 1 and 20): 6
* * * * * *
Question 8
Modify question 7 so that when the user enters a number, the output is a square of stars instead of a line. For this, you will need to use nested for loops (one for loop inside of another for loop). Name your program Question8
Example Output
Please enter the number of stars you would like(between 1 and 20): 6
* * * * * *
* * * * * *
* * * * * *
* * * * * *
* * * * * *
* * * * * *
Question 1
Redo Question 1 from exercise 3, the count down question, but instead of using a while loop, use a for loop. Name your program Question1
Example Output
10
9
8
7
6
5
4
3
2
1
0
Blast Off!
Question 2
Write a program that will ask the user for a message and the number of times they want that message displayed. Then output the message that number of times. Name your program Question2
Example Output
What message would you like displayed? I Like Cake
How many times would you like to display your message? 4
I Like Cake
I Like Cake
I Like Cake
I Like Cake
Question 3
Create a program that will ask the user for a number and then print out a list of number from 1 to the number entered and the square of the number. Name your program Question3
Example Output
What number would you like to go up to? 5
1 squared is 1
2 squared is 4
3 squared is 9
4 squared is 16
5 squared is 25
Question 4
Write a program to output a backwards count by 5s from 100 down a number between 100 and 50 that is input before the countdown begins. The program should stop when the count would be greater than the number input. Name your program Question4
Example Output
Enter a number between 50 and 100 to stop at: 82
100
95
90
85
Question 5
When $1000 is deposited at 5 percent simple interest, the amount grows by $50 each year (i.e. the interest is calculated on the original principal of $1000 only every time). When money is invested at 5 percent compound interest, then the amount at the end of the year is 1.05 times the amount at the beginning of that year. Write a program to display the amounts for 10 years for a $1000 deposit at 5 percent simple and compound interest. The first few lines displayed in the picture box should appear as shown below. Save your program as Question5.
Amount Amount
Simple Compound
Year Interest Interest
1 $1,050.00 $1,050.00
2 $1,100.00 $1,102.50
3 $1,150.00 $1,157.63
Question 6
Rewrite problem 5 so that the user can enter their own starting principal, their own interest rate and the number of years that they want displayed on the screen. At the end display a grand total for each column. Save this program as Question6.
Question 7
Write a program that will request a number in the range 1 to 20 and display a row of that many stars (asterisks). Hint: when you print, you can tell Java not to put any spaces between this line, and the next line by using the print() command instead of println(). For instance,
System.out.print("Hello ");
System.out.println("World");
will output to the screen "Hello World" all on one line.
Name your program Question7
Example Output
Please enter the number of stars you would like(between 1 and 20): 6
* * * * * *
Question 8
Modify question 7 so that when the user enters a number, the output is a square of stars instead of a line. For this, you will need to use nested for loops (one for loop inside of another for loop). Name your program Question8
Example Output
Please enter the number of stars you would like(between 1 and 20): 6
* * * * * *
* * * * * *
* * * * * *
* * * * * *
* * * * * *
* * * * * *