chaibs

arrays exc1-10

Dec 27th, 2017
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.39 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class arrex1 {
  4.  
  5.     public static void main(String[] args) {
  6.         int[] arr = new int[10];
  7.  
  8.         Scanner s = new Scanner(System.in);
  9.  
  10.         System.out.println("Please enter 10 numbers:");
  11.         for (int i = 0; i < arr.length; i += 1) {// loop for input from user to the array
  12.             arr[i] = s.nextInt();
  13.         }
  14.  
  15.         for (int i = 0; i < arr.length; i += 1) {// loop for output from the array
  16.             if (arr[i] % 2 == 0) {// checking if the number in the current index is even
  17.                 System.out.print(arr[i] + " ");// prints even numbers
  18.             }
  19.         }
  20.     }
  21.  
  22. }
  23. ------------------------------------------------------------------------------------------------------
  24. import java.util.Scanner;
  25.  
  26. public class arrex2 {
  27.  
  28.     public static void main(String[] args) {
  29.         char[] arr = new char[10];
  30.  
  31.         Scanner s = new Scanner(System.in);
  32.  
  33.         System.out.println("Please enter 10 characters:");
  34.         for (int i = 0; i < arr.length; i += 1) {// loop for input from user to the array
  35.             arr[i] = s.next().charAt(0);
  36.         }
  37.  
  38.         for (int i = 0; i < arr.length; i += 1) {// loop for output from the array
  39.             if (arr[i] <= 'Z' && arr[i] >= 'A') {// checking if the chars in the current index is capital letter
  40.                 System.out.print(i + " ");// prints the index
  41.             }
  42.         }
  43.  
  44.     }
  45.  
  46. }
  47. ----------------------------------------------------------------------------------------------
  48. import java.util.Scanner;
  49.  
  50. public class arrex3 {
  51.  
  52.     public static void main(String[] args) {
  53.         int[] arr = new int[10];
  54.  
  55.         for (int i = 0, count = 0; i < arr.length; count += 1) {
  56.             if (count % 3 == 0) {// checking if count is divided in 3
  57.                 arr[i] = count;// adding the number to the array
  58.                 i += 1;// inc the index
  59.             }
  60.         }
  61.  
  62.         for (int i = 0; i < arr.length; i += 1) {// loop for output from the array
  63.             System.out.print(arr[i] + " ");// prints the numbers that divided in 3
  64.         }
  65.  
  66.     }
  67.  
  68. }
  69. ----------------------------------------------------------------------------------------------
  70. import java.util.Scanner;
  71.  
  72. public class arrex4 {
  73.  
  74.     public static void main(String[] args) {
  75.         int[] arr = new int[10];
  76.  
  77.         Scanner s = new Scanner(System.in);
  78.  
  79.         System.out.println("Please enter 10 numbers:");
  80.         for (int i = 0; i < arr.length; i += 1) {// loop for input from user to the array
  81.             arr[i] = s.nextInt();
  82.         }
  83.  
  84.         for (int i = 0; i < arr.length; i += 1) {// loop for output from the array
  85.             if (i % 2 == 0) {// checking if the current index is even
  86.                 arr[i] += 1;
  87.             }
  88.         }
  89.  
  90.         for (int i = 0; i < arr.length; i += 1) {// loop for output from the array
  91.             System.out.print(arr[i] + " ");// prints the numbers
  92.         }
  93.     }
  94.  
  95. }
  96. --------------------------------------------------------------------------------------
  97. import java.util.Scanner;
  98.  
  99. public class arrex5 {
  100.  
  101.     public static void main(String[] args) {
  102.         int[] arr = new int[10];
  103.  
  104.         Scanner s = new Scanner(System.in);
  105.  
  106.         System.out.println("Please enter 10 numbers:");
  107.         for (int i = 0; i < arr.length; i += 1) {// loop for input from user to the array
  108.             arr[i] = s.nextInt();
  109.         }
  110.  
  111.         for (int i = 0; i < arr.length; i += 1) {// loop for output from the array
  112.             if (i % 2 == 0) {// checking if the current index is even
  113.                 arr[i] += 1;
  114.             }
  115.         }
  116.  
  117.         for (int i = 0; i < arr.length; i += 1) {
  118.             if (i % 3 == 0) {// checking if count is divided in 3
  119.                 arr[i] -= 1;// adding the number to the array
  120.             }
  121.         }
  122.        
  123.         for (int i = 0; i < arr.length; i += 1) {// loop for output from the array
  124.             System.out.print(arr[i] + " ");// prints the numbers
  125.         }
  126.  
  127.     }
  128.  
  129. }
  130. -------------------------------------------------------------------------------------------
  131. import java.util.Scanner;
  132.  
  133. public class arrex6 {
  134.  
  135.     public static void main(String[] args) {
  136.         char[] arr = new char[10];
  137.         int count = 0;
  138.  
  139.         Scanner s = new Scanner(System.in);
  140.  
  141.         System.out.println("Please enter 10 characters:");
  142.         for (int i = 0; i < arr.length; i += 1) {// loop for input from user to the array
  143.             arr[i] = s.next().charAt(0);
  144.         }
  145.  
  146.         System.out.println("Please enter anther character:");
  147.         char ch = s.next().charAt(0);// getting anther char from user
  148.  
  149.         for (int i = 0; i < arr.length; i += 1) {
  150.             if (ch == arr[i]) {// checking if the char is found in the array
  151.                 count += 1;// count the time we found the char in the array
  152.             }
  153.         }
  154.  
  155.         System.out.println("The ch '" + ch + "' was found in the array " + count + " times");// result
  156.     }
  157.  
  158. }
  159. --------------------------------------------------------------------------------------------------------
  160. import java.util.Scanner;
  161.  
  162. public class arrex7 {
  163.  
  164.     public static void main(String[] args) {
  165.         int[] arr1 = new int[5];
  166.         int[] arr2 = new int[5];
  167.  
  168.         Scanner s = new Scanner(System.in);
  169.  
  170.         System.out.println("Please enter 10 numbers:");
  171.         for (int i = 0; i < arr1.length; i += 1) {// loop for input from user to the array1
  172.             arr1[i] = s.nextInt();
  173.         }
  174.  
  175.         System.out.println("Please enter 10 numbers:");
  176.         for (int i = 0; i < arr2.length; i += 1) {// loop for input from user to the array2
  177.             arr2[i] = s.nextInt();
  178.         }
  179.  
  180.         for (int i = 0; i < arr1.length; i += 1) {
  181.             for (int j = 0; j < arr2.length; j += 1) {
  182.                 if (arr1[i] == arr2[j]) {// checking if the numbers in the array are matched
  183.                     System.out.println("The index from arr1 is " + i);// result for the first array
  184.                     System.out.println("The index from arr2 is " + j);// result for the second array
  185.                 }
  186.             }
  187.         }
  188.     }
  189.  
  190. }
  191. -----------------------------------------------------------------------------------------------------------
  192. import java.util.Scanner;
  193.  
  194. public class arrex8 {
  195.  
  196.     public static void main(String[] args) {
  197.         int[] arr1 = new int[5];
  198.         int[] arr2 = new int[5];
  199.         int[] arr3 = new int[5];
  200.  
  201.         Scanner s = new Scanner(System.in);
  202.  
  203.         System.out.println("Please enter 5 numbers:");
  204.         for (int i = 0; i < arr1.length; i += 1) {// loop for input from user to the array1
  205.             arr1[i] = s.nextInt();
  206.         }
  207.  
  208.         System.out.println("Please enter 5 numbers:");
  209.         for (int i = 0; i < arr2.length; i += 1) {// loop for input from user to the array2
  210.             arr2[i] = s.nextInt();
  211.         }
  212.  
  213.         for (int i = 0; i < arr3.length; i += 1) {
  214.             arr3[i] = arr1[i] + arr2[i];// summing the numbers from the first and second array
  215.         }
  216.  
  217.         System.out.println();
  218.  
  219.         for (int i = 0; i < arr3.length; i += 1) {// loop for output from the array
  220.             System.out.print(arr3[i] + " ");// prints the numbers
  221.         }
  222.     }
  223.  
  224. }
  225. ------------------------------------------------------------------------------------------
  226. import java.util.Scanner;
  227.  
  228. public class arrex9 {
  229.  
  230.     public static void main(String[] args) {
  231.         char[] arr = new char[5];
  232.         boolean b = false;
  233.  
  234.         Scanner s = new Scanner(System.in);
  235.  
  236.         System.out.println("Please enter 5 characters:");
  237.         for (int i = 0; i < arr.length; i += 1) {// loop for input from user to the array
  238.             arr[i] = s.next().charAt(0);
  239.         }
  240.  
  241.         for (int i = 0; i < arr.length; i += 1) {
  242.             for (int j = 1; j < arr.length; j += 1) {
  243.                 if (arr[i] == arr[j]) {// checking if the character is the same in all the array
  244.                     b = true;
  245.                 } else {
  246.                     b = false;
  247.                     j = i = arr.length;
  248.                 }
  249.             }
  250.         }
  251.  
  252.         if (b == true) {
  253.             System.out.println("All the character in the array are the same");// result if all the characters are the
  254.                                                                                 // same
  255.         } else {
  256.             System.out.println("All the character in the array are not the same");// result if all the characters are
  257.                                                                                     // not the same
  258.         }
  259.     }
  260.  
  261. }
  262. -------------------------------------------------------------------------------------------------------
  263. import java.util.Scanner;
  264.  
  265. public class arrex10 {
  266.  
  267.     public static void main(String[] args) {
  268.         int[] arr1 = new int[5];
  269.         boolean b = false;
  270.  
  271.         Scanner s = new Scanner(System.in);
  272.  
  273.         System.out.println("Please enter 5 numbers:");
  274.         for (int i = 0; i < arr1.length; i += 1) {// loop for input from user to the array1
  275.             arr1[i] = s.nextInt();
  276.         }
  277.  
  278.         for (int i = 1; i < arr1.length; i += 1) {
  279.             if (arr1[i] > arr1[i - 1]) {// checking if all the number is bigger then his previous in the array
  280.                 b = true;
  281.             } else {
  282.                 b = false;
  283.                 i = arr1.length;
  284.             }
  285.         }
  286.  
  287.         if (b == true) {
  288.             System.out.println("All the numbers are bigger then their previous");// result if all the numbers are bigger
  289.                                                                                     // then their previous
  290.  
  291.         } else {
  292.             System.out.println("All the numbers are not bigger then their previos");// result if all the numbers are not
  293.                                                                                     // bigger then their previous
  294.  
  295.         }
  296.     }
  297.  
  298. }
Advertisement
Add Comment
Please, Sign In to add comment