Răspuns :
Răspuns:
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Vector {
private static final Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
List<Integer> listOfNumbers = new ArrayList<>();
System.out.print("Introduceti numarul de elemente n= ");
int n = scanner.nextInt();
for (int i = 0;i < n; i++) {
System.out.print("Introduceti elementul nr-ul " + (i + 1) + ": ");
listOfNumbers.add(scanner.nextInt());
}
System.out.println("Sirul introdus: " + listOfNumbers);
System.out.println("a)Au fost introduse " + countPrimeElements(listOfNumbers) + " elemente prime");
System.out.print("Introduceti valoarea lui k: ");
int k = scanner.nextInt();
displayElementsWithKDivisors(listOfNumbers, k);
displayJoinedEvenElements(listOfNumbers);
displaySumOfElements(listOfNumbers);
displayListSymmetry(listOfNumbers);
displaySumOfSquaresOfElements(listOfNumbers);
displayElementsThatAreSmallerThanReversedValue(listOfNumbers);
displayMinimalDigitOfEachElement(listOfNumbers);
}
private static boolean isPrime(int num) {
for (int i = 2;i <= num / 2; i++) {
if (num % i == 0) {
return false;
}
}
return true;
}
private static int countPrimeElements(List<Integer> list) {
int counter = 0;
for (int element : list) {
if (isPrime(element)) {
counter++;
}
}
return counter;
}
private static int countNumberOfDivisors(int num) {
int counter = 0;
for (int i = 2;i <= num / 2; i++) {
counter = num % i == 0 ? counter + 1 : counter;
}
return counter;
}
private static void displayElementsWithKDivisors(List<Integer> list, int k) {
System.out.print("b) Elementele cu " + k + " divizori: ");
for (int element : list) {
if (countNumberOfDivisors(element) == k) {
System.out.print(element + " ");
}
}
}
private static void displayJoinedEvenElements(List<Integer> list) {
System.out.println("\nc) Elementele pare alaturate:");
for (int i = 0;i < list.size() - 1; i++) {
if (list.get(i) % 2 == 0 && list.get(i + 1) % 2 == 0) {
System.out.println(list.get(i) + " " + list.get(i + 1));
}
}
}
private static void displaySumOfElements(List<Integer> list) {
System.out.print("d) Suma elementelor sirului: ");
int sum = 0;
for (int element : list) {
sum += element;
}
System.out.println(sum);
}
private static boolean isListSymmetric(List<Integer> list) {
for (int i = 0;i < list.size(); i++) {
if (!list.get(i).equals(list.get(list.size() - i - 1))) {
return false;
}
}
return true;
}
private static void displayListSymmetry(List<Integer> list) {
if (isListSymmetric(list)) {
System.out.println("e)Sirul introdus este simetric");
} else {
System.out.println("e)Sirul introdus nu este simetric");
}
}
private static void displaySumOfSquaresOfElements(List<Integer> list) {
int sum = 0;
for (int element : list) {
sum += Math.pow(element, 2);
}
System.out.println("f)Suma patratelor elementelor sirului este: " + sum);
}
private static void displayElementsThatAreSmallerThanReversedValue(List<Integer> list) {
System.out.println("g)Elementele a caror rasturnat este mai mare ca elementul dat:");
for (int element : list) {
if (element < getReversedNumber(element)) {
System.out.print(element + " ");
}
}
}
private static int getReversedNumber(int num) {
int result = 0;
while (num != 0) {
result = result * 10 + num % 10;
num /= 10;
}
return result;
}
private static void displayMinimalDigitOfEachElement(List<Integer> list) {
System.out.println("\nh)Cifra minima a fiecarui element al sirului: ");
for (int element : list) {
System.out.println(element + " -> " + getMinimalDigit(element));
}
}
private static int getMinimalDigit(int num) {
int min = 9;
while (num != 0) {
if (num % 10 < min) {
min = num % 10;
}
num /= 10;
}
return min;
}
}
Explicație: