Operators. Tasks

Author: Tatyana Milkina

1. Even or Odd

  1. Pass a number as a program argument.
  2. If the number is odd, print it.
  3. Use the if statement.
  4. Use Integer.parseInt() to convert the String to int.

Solution on Patreon.

2. Number between 0 and 10

  1. Use the Scanner class to read an integer.
  2. If the number is between 0 and 10, print “Positive number less than 10 or zero”. Otherwise, print “Positive number greater than 10 or negative”.
  3. Use the if-else statement.

Solution on Patreon.

3. Days of the Week

  1. Pass a number from 1 to 7 as a program argument.
  2. Print “Monday” if the number is 1, “Tuesday” for 2, etc. Print “Weekend” for 6 or 7.
  3. Use the if-else-if structure.

Watch the solution in the video.

Solution on Patreon.

4. Days of the Week with switch

Rewrite the previous task using the switch statement.

Watch the solution in the video.

Solution on Patreon.

5. Print 10 Lines Using while

Print 10 lines: “Task1”, “Task2”, ..., “Task10”. Use the while loop.

Watch the solution in the video.

Solution on Patreon.

6. Print Numbers Using do-while

Print all numbers from 1 to 100 divisible by 5 using the do-while loop.

Watch the solution in the video.

Solution on Patreon.

7. Print 5 Characters Using for

Print 5 characters in a row starting with ‘h’. Use the for loop.

Watch the solution in the video.

Solution on Patreon.

8. Garland, Part 2

Extend the Garland Part 1 task with the following:

  1. Use Scanner to input a number from 1 to 4.
  2. 1 – blink method, 2 – check first bulb, 3 – running light method, 4 – print garland state.
  3. Use switch statement for mode selection.
  4. Update blink method to blink 10 times (use for loop).
  5. Update running light method to run 10 times (use for loop).

Solution on Patreon.

9. Factorial

Calculate factorial using a for loop. Example:

n! = 1*2*...*n;
0! = 1;
5! = 1*2*3*4*5;

Generate a random number n between 0 and 5 using Math.random().

Solution on Patreon.

Курс 'Java для начинающих' на Udemy Курс 'Java для начинающих' на Udemy
Read also:
Comments