Advertisement
Guest User

BlablaJava

a guest
Nov 22nd, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.22 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.lang.*;
  3. public class EX_01{
  4.  
  5. public static void main(String[] args) {
  6. // TODO Auto-generated method stub
  7.  
  8. //EX_01
  9. {
  10.  
  11. int arr[] = new int[3];
  12. Scanner sc = new Scanner(System.in);
  13. System.out.println("Hello!\nPlease enter 3 numbers:");
  14.  
  15. for(int i=0;i<3;i++)
  16. {
  17. arr[i]=sc.nextInt();
  18. }
  19.  
  20. int pow1=arr[0]*arr[0], pow2=arr[1]*arr[1], pow3=arr[2]*arr[2];
  21.  
  22. if (pow1==pow2+pow3||pow2==pow1+pow3||pow3==pow1+pow2)
  23. {
  24. System.out.println("Pythagorean triplet!");
  25. }
  26. else
  27. {
  28. System.out.println("NO!");
  29. }
  30. }
  31.  
  32. //EX_02
  33. {
  34. int num,print=1,counter=0;
  35. System.out.println("\nHello!\nPlease enter a number (bigger then 1):");
  36. Scanner sc = new Scanner(System.in);
  37. num= sc.nextInt();
  38.  
  39. for (int i=0;i<num*num;i++)
  40. {
  41. System.out.print(print+"\t");
  42. print++;
  43. counter++;
  44. if (counter==num)
  45. {
  46. counter=0;
  47. System.out.println();
  48. }
  49. }
  50. }
  51.  
  52. //EX_03
  53. {
  54. System.out.println("\nHello!\nPlease give me a string:");
  55. Scanner sc= new Scanner(System.in);
  56. String str= sc.nextLine();
  57. int begin=0, end=str.length()-1;
  58.  
  59. while(begin<end)
  60. {
  61. if (str.charAt(begin)!=str.charAt(end))
  62. {
  63. System.out.println("Not a palindrome!");
  64. break;
  65. }
  66. begin++;
  67. end--;
  68. }
  69.  
  70. if (begin==end)
  71. {
  72. System.out.println("Palindrome with middle!");
  73. }
  74. if (begin>end)
  75. {
  76. System.out.println("Palindrome without middle!");
  77. }
  78. }
  79.  
  80. //EX_04
  81. {
  82. int num, counter=0;
  83. float sum=0;
  84. Scanner sc= new Scanner(System.in);
  85. System.out.println("\nHello!\nPlease enter a size for the array: ");
  86. num=sc.nextInt();
  87. int[] arr= new int[num];
  88. System.out.println("Please enter " + num + " numbers to the array:");
  89.  
  90. for (int i=0;i<num;i++)
  91. {
  92. arr[i]=sc.nextInt();
  93.  
  94. if (arr[i]%2==0)
  95. {
  96. sum+=arr[i];
  97. counter++;
  98. }
  99. }
  100. System.out.println("The avarage of the even numbers in the array is: " + sum/counter);
  101. }
  102.  
  103. //EX_05
  104. {
  105. int[][] arr;
  106. int num,sum=0;
  107. Scanner sc = new Scanner(System.in);
  108. System.out.println("\nHello!\nPlease enter a number: ");
  109. num= sc.nextInt();
  110. arr= new int[num][num];
  111.  
  112. System.out.println("The Matrix is:");
  113.  
  114. for(int i=0;i<num;i++)
  115. {
  116. for(int j=0;j<num;j++)
  117. {
  118. arr[i][j]=(int) (Math.random()*100+1);
  119. System.out.print(arr[i][j]+ "\t");
  120. if(i==j)
  121. {
  122. sum+=arr[i][j];
  123. }
  124. }
  125. System.out.println();
  126. }
  127. System.out.println("\nThe Sum of the main diagonal is: " + sum);
  128. }
  129. }
  130. }
  131.  
  132.  
  133. /*
  134. ------OUTPUT EXAMPLE------
  135. Hello!
  136. Please enter 3 numbers:
  137. 5
  138. 3
  139. 4
  140. Pythagorean triplet!
  141.  
  142. Hello!
  143. Please enter a number (bigger then 1):
  144. 3
  145. 1 2 3
  146. 4 5 6
  147. 7 8 9
  148.  
  149. Hello!
  150. Please give me a string:
  151. BlalB
  152. Palindrome with middle!
  153.  
  154. Hello!
  155. Please enter a size for the array:
  156. 4
  157. Please enter 4 numbers to the array:
  158. 3
  159. 5
  160. 6
  161. 6
  162. The avarage of the even numbers in the array is: 6.0
  163.  
  164. Hello!
  165. Please enter a number:
  166. 4
  167. The Matrix is:
  168. 31 28 82 21
  169. 98 54 52 12
  170. 15 57 46 27
  171. 22 60 48 80
  172.  
  173. The Sum of the main diagonal is: 211
  174.  
  175. -------------------------------------------------------------
  176.  
  177. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement