Advertisement
Guest User

Untitled

a guest
Dec 4th, 2016
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.80 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.util.Arrays;
  3. public class VDN1
  4. {
  5.  
  6. public static void main(String[] args)
  7. {
  8. System.out.print("Pozdravljeni, koliko stevil zelite vnesti? ");
  9. Scanner user_input = new Scanner (System.in);
  10. int steviloStevil = user_input.nextInt();
  11. int polje[] = new int [steviloStevil];
  12.  
  13.  
  14. for (int i = 0; i < polje.length; i++)
  15. {
  16. System.out.print("Vnesite " + (i+1) + ". stevilo: ");
  17. polje[i] = user_input.nextInt();
  18. }
  19. System.out.println("Vseh elementov: " + steviloStevil);
  20.  
  21. int steviloSodihStevil = 0;
  22. int steviloLihihStevil = 0;
  23. for (int a = 0; a < polje.length; a++)
  24. {
  25. if (polje[a] % 2 == 0)
  26. {
  27. steviloSodihStevil++;
  28. }
  29. else
  30. {
  31. steviloLihihStevil++;
  32. }
  33. }
  34. System.out.println("Stevilo sodih stevil: " + steviloSodihStevil);
  35. System.out.println("Stevilo lihih stevil: " + steviloLihihStevil);
  36.  
  37. int najvecjeStevilo = polje[0];
  38. for (int i = 0; i < polje.length; i++)
  39. {
  40. if (polje[i] > najvecjeStevilo)
  41. {
  42. najvecjeStevilo = polje[i];
  43. }
  44. }
  45. System.out.println("Najvecje stevilo: " + najvecjeStevilo);
  46.  
  47. int vsotaVsehStevil = 0;
  48. for (int i = 0; i < polje.length; i++)
  49. {
  50. vsotaVsehStevil += polje[i];
  51. }
  52. System.out.println("Vsota vseh stevil: " + vsotaVsehStevil);
  53.  
  54. System.out.println("Povprecje vseh stevil: " + (double)vsotaVsehStevil/steviloStevil);
  55. //
  56. Arrays.sort(polje);
  57. for (int i = steviloStevil -1; i >= 0; i--)
  58. {
  59. if (i != 0)
  60. {
  61. System.out.print(polje[i] + ", ");
  62. }
  63. else
  64. {
  65. System.out.println(polje[i] + ".");
  66. }
  67. }
  68.  
  69.  
  70. for (int i = 1; i < polje.length; i++)
  71. {
  72. if (polje[i] == polje[i - 1])
  73. {
  74. steviloStevil = steviloStevil - 1;
  75. }
  76. }
  77. System.out.println("Stevilo razlicnih stevil: " + steviloStevil);
  78.  
  79. //praštevila
  80. int primeCounter = 0;
  81. for (int i = 0; i < polje.length; i++)
  82. {
  83. if (isPrime(polje[i]))
  84. {
  85. primeCounter++;
  86. }
  87. }
  88. System.out.println("Stevilo prastevil: " + primeCounter);
  89.  
  90. Arrays.sort(polje);
  91. double mediana;
  92. if (polje.length % 2 == 0)
  93. {
  94. mediana = ((double) polje[polje.length / 2] + (double)polje[polje.length/2 - 1]) / 2;
  95. }
  96. else
  97. {
  98. mediana = (double) polje[polje.length / 2];
  99. }
  100. System.out.println("Mediana: " + mediana);
  101.  
  102.  
  103. int minValue = najvecjeStevilo;
  104. int secondLowest = najvecjeStevilo;
  105. for (int i = 0; i < polje.length; i++)
  106. {
  107. if (polje[i] < minValue)
  108. {
  109. secondLowest = minValue;
  110. minValue = polje[i];
  111. }
  112. else if (polje[i] < secondLowest)
  113. {
  114. secondLowest = polje[i];
  115. }
  116. }
  117. System.out.println("Drugo najmanjsa vrednost stevil: " + secondLowest);
  118.  
  119.  
  120. int najvecjePrasteviloInPalindrom = getArrayIsPrimeAndPalindrome(najvecjeStevilo);
  121. System.out.println("Največje praštevilo in hkrati palindrom: " + najvecjePrasteviloInPalindrom);
  122.  
  123.  
  124. }
  125. public static int getArrayIsPrimeAndPalindrome(int najvecjeStevilo)
  126. {
  127. for (int i = najvecjeStevilo; i > 0; i--)
  128. {
  129. if (isPrime(i) && isPalindrome(i)) return i;
  130. }
  131. return 0;
  132. }
  133. public static boolean isPalindrome(int n)
  134. {
  135. int num = 0;
  136. n = num;
  137. int rev = 0;
  138. while (num > 0)
  139. {
  140. int dig = num % 10;
  141. rev = rev * 10 + dig;
  142. num = num / 10;
  143. }
  144. if (n==rev) return true;
  145. else return false;
  146. }
  147. public static boolean isPrime(int number)
  148. {
  149. if (number == 1) return false;
  150. if (number == 2) return true;
  151. if (number % 2 == 0) return false;
  152. for (int i = 3; i < number; i += 2)
  153. {
  154. if (number % i == 0) return false;
  155. }
  156. return true;
  157. }
  158. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement