1. Loops
Write a program which calculates and outputs the sum of all the integers (inclusive) between 1 and 5. Solve the task using:
- while loop
- do ... while loop
- for loop
Save each solution in a separate file.
2. Choose the loop
Choose an appropriate loop type for each task and write the program.
- Output integers from 1 to 5.
- Create 2 variables: sum and limit; then add value 2 to variable sum until sum becomes greater than limit.
- Output the following text using a loop: I like Java. The program has to print the message out at least once irrespectively of the loop conditions.
3. Input
Write a program that reads three edges for a triangle and determines whether the input is valid or not. (Hint: check the sum of the edges).
Example of the program output:
Can edges 1, 2 and 10 form a triangle? False
Another example of the program output:
Can edges 4, 5 and 6 form a triangle? True
4. Input with a loop
Write a program that reads an integer and checks whether it is even. The program must terminate if the user enters 0.
Example of the program output: Is 25 an even number? False Is 100 an even number? True Is 0 an even number? I am done.
5. Kilos and Pounds
Write a program that displays the following table (note: 1 kg = 2.2 pounds; if you want to be more precise 1 kg = 2.20462262185 pounds). The output of the program should look like this:
Kg Pounds 1 2.2 3 6.6 ... 197 433.4 199 437.8
To output the result as a table, format the output. To create columns for the table use tab symbol \t
. For example, System.out.println("abc"+"\t"+"def")
; in such case string def will be allocated into a new column. To format floating point numbers, use System.out.printf
. For example, System.out.printf("%.1f",1234.45)
will display double numbers with one digit after the comma.
Upgrade the program so that the user is prompted for the range of the kilos (a starting value of the calculations, a step, an ending value of the calculation).
6. Sum of a series
Write a program that calculates and prints out the sum of the following series:
Upgrade the program so that the user is prompted for the number of the first terms to be summed.