Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- import java.util.Arrays;
- public class VDN1
- {
- public static void main(String[] args)
- {
- System.out.print("Pozdravljeni, koliko stevil zelite vnesti? ");
- Scanner user_input = new Scanner (System.in);
- int steviloStevil = user_input.nextInt();
- int polje[] = new int [steviloStevil];
- for (int i = 0; i < polje.length; i++)
- {
- System.out.print("Vnesite " + (i+1) + ". stevilo: ");
- polje[i] = user_input.nextInt();
- }
- System.out.println("Vseh elementov: " + steviloStevil);
- int steviloSodihStevil = 0;
- int steviloLihihStevil = 0;
- for (int a = 0; a < polje.length; a++)
- {
- if (polje[a] % 2 == 0)
- {
- steviloSodihStevil++;
- }
- else
- {
- steviloLihihStevil++;
- }
- }
- System.out.println("Stevilo sodih stevil: " + steviloSodihStevil);
- System.out.println("Stevilo lihih stevil: " + steviloLihihStevil);
- int najvecjeStevilo = polje[0];
- for (int i = 0; i < polje.length; i++)
- {
- if (polje[i] > najvecjeStevilo)
- {
- najvecjeStevilo = polje[i];
- }
- }
- System.out.println("Najvecje stevilo: " + najvecjeStevilo);
- int vsotaVsehStevil = 0;
- for (int i = 0; i < polje.length; i++)
- {
- vsotaVsehStevil += polje[i];
- }
- System.out.println("Vsota vseh stevil: " + vsotaVsehStevil);
- System.out.println("Povprecje vseh stevil: " + (double)vsotaVsehStevil/steviloStevil);
- //
- Arrays.sort(polje);
- for (int i = steviloStevil -1; i >= 0; i--)
- {
- if (i != 0)
- {
- System.out.print(polje[i] + ", ");
- }
- else
- {
- System.out.println(polje[i] + ".");
- }
- }
- for (int i = 1; i < polje.length; i++)
- {
- if (polje[i] == polje[i - 1])
- {
- steviloStevil = steviloStevil - 1;
- }
- }
- System.out.println("Stevilo razlicnih stevil: " + steviloStevil);
- //praštevila
- int primeCounter = 0;
- for (int i = 0; i < polje.length; i++)
- {
- if (isPrime(polje[i]))
- {
- primeCounter++;
- }
- }
- System.out.println("Stevilo prastevil: " + primeCounter);
- Arrays.sort(polje);
- double mediana;
- if (polje.length % 2 == 0)
- {
- mediana = ((double) polje[polje.length / 2] + (double)polje[polje.length/2 - 1]) / 2;
- }
- else
- {
- mediana = (double) polje[polje.length / 2];
- }
- System.out.println("Mediana: " + mediana);
- int minValue = najvecjeStevilo;
- int secondLowest = najvecjeStevilo;
- for (int i = 0; i < polje.length; i++)
- {
- if (polje[i] < minValue)
- {
- secondLowest = minValue;
- minValue = polje[i];
- }
- else if (polje[i] < secondLowest)
- {
- secondLowest = polje[i];
- }
- }
- System.out.println("Drugo najmanjsa vrednost stevil: " + secondLowest);
- int najvecjePrasteviloInPalindrom = getArrayIsPrimeAndPalindrome(najvecjeStevilo);
- System.out.println("Največje praštevilo in hkrati palindrom: " + najvecjePrasteviloInPalindrom);
- }
- public static int getArrayIsPrimeAndPalindrome(int najvecjeStevilo)
- {
- for (int i = najvecjeStevilo; i > 0; i--)
- {
- if (isPrime(i) && isPalindrome(i)) return i;
- }
- return 0;
- }
- public static boolean isPalindrome(int n)
- {
- int num = 0;
- n = num;
- int rev = 0;
- while (num > 0)
- {
- int dig = num % 10;
- rev = rev * 10 + dig;
- num = num / 10;
- }
- if (n==rev) return true;
- else return false;
- }
- public static boolean isPrime(int number)
- {
- if (number == 1) return false;
- if (number == 2) return true;
- if (number % 2 == 0) return false;
- for (int i = 3; i < number; i += 2)
- {
- if (number % i == 0) return false;
- }
- return true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement