Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2020
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.14 KB | None | 0 0
  1. public class task7Lesson2 {
  2.  
  3. public static void main(String[] args) {
  4. // Задача 1. Создайте массив из всех чётных чисел от 2 до 20 и выведите элементы
  5. // массива на экран сначала в строку,
  6. // отделяя один элемент от другого пробелом, а затем в столбик (отделяя один
  7. // элемент от другого началом новой строки).
  8. // Перед созданием массива подумайте, какого он будет размера.
  9. // 2 4 6 … 18 20
  10. // 2
  11. // 4
  12. // 6
  13. // …
  14. // 20
  15. int f = 0;
  16. int e = 0;
  17. int arr[] = new int[10];
  18. for (int i = 2; i <= 20; i++) {
  19. if (i % 2 == 0) {
  20. arr[f] = i;
  21. f++;
  22. }
  23. }
  24. int arr1[] = new int[10];
  25. for (int t = 2; t <= 20; t++) {
  26. if (t % 2 == 0) {
  27. arr1[e] = t;
  28. e++;
  29. }
  30. }
  31. for (int x = 0; x < arr.length; x++) {
  32. System.out.print(arr[x] + " ");
  33. }
  34. System.out.println();
  35. for (int r = 0; r < arr1.length + 1; r++) {
  36. System.out.println(arr1[r] + " ");
  37. }
  38. }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement