chaibs

functions-hw1-2

Dec 31st, 2017
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.29 KB | None | 0 0
  1. public class funchw1 {
  2.  
  3.     public static void main(String[] args) {
  4.         int dig = 0, num = 0, res = 0;
  5.         int[] arr = new int[5];
  6.  
  7.         Scanner s = new Scanner(System.in);
  8.  
  9.         System.out.println("Please enter a number:");
  10.         num = s.nextInt();// getting a number from user
  11.  
  12.         System.out.println("Please enter a single number between 1-9:");
  13.         dig = s.nextInt();// getting a single number from user
  14.  
  15.         res = countDig(num, dig);// calling function
  16.  
  17.         System.out.printf("The number of time %d was found is %d\n\n", dig, res);// result
  18.  
  19.         System.out.println("Please enter a bunch of numbers:");
  20.         for (int i = 0; i < arr.length; i += 1) {
  21.             arr[i] = s.nextInt();// getting a bunch of numbers from user
  22.         }
  23.  
  24.         System.out.println("Please enter a single number between 1-9:");
  25.         dig = s.nextInt();// getting a single number from user
  26.  
  27.         res = countDig2(dig, arr);// calling function
  28.  
  29.         System.out.printf("The number of time %d was found is %d", dig, res);// result
  30.     }
  31.  
  32.     public static int countDig(int num, int dig) {
  33.         int count = 0;
  34.         while (num > 0) {// as long as number is not 0
  35.             if (num % 10 == dig) {// checking if the rightest number is the same as the digit
  36.                 count += 1;// count the time we found the a match
  37.             }
  38.             num /= 10;// removing the rightest number
  39.         }
  40.         return count;// returning count to main
  41.     }
  42.  
  43.     public static int countDig2(int dig, int[] arr) {
  44.         int count = 0, d = 0;
  45.  
  46.         for (int i = 0; i < arr.length; i += 1) {
  47.             while (arr[i] > 0) {// checking the number from the array
  48.                 d = arr[i];// saving the number from the array
  49.                 if (d % 10 == dig) {// checking if the rightest number is the same as the digit
  50.                     count += 1;// count the time we found the a match
  51.                 }
  52.                 arr[i] /= 10;// removing the rightest number
  53.             }
  54.         }
  55.         return count;// returning count to main
  56.     }
  57. }
  58. --------------------------------------------------------------------------------------------------------------
  59.  
  60. public class funchw2 {
  61.  
  62.     public static void main(String[] args) {
  63.         int[] arr1 = new int[5];
  64.         int[] arr2 = new int[5];
  65.         int[] res = new int[5];
  66.  
  67.         Scanner s = new Scanner(System.in);
  68.  
  69.         System.out.println("Please enter a bunch of numbers to the first array:");
  70.         for (int i = 0; i < arr1.length; i += 1) {
  71.             arr1[i] = s.nextInt();// getting a bunch of numbers from user
  72.         }
  73.  
  74.         System.out.println("Please enter a bunch of numbers to the second array:");
  75.         for (int i = 0; i < arr2.length; i += 1) {
  76.             arr2[i] = s.nextInt();// getting a bunch of numbers from user
  77.         }
  78.  
  79.         res = createArr(arr1, arr2);// calling the function
  80.  
  81.         for (int i = 0; i < res.length; i += 1) {
  82.             if (res[i] != 0) {// check if there are 0 in the array
  83.                 System.out.println(res[i]);// result
  84.             }
  85.         }
  86.     }
  87.  
  88.     public static int[] createArr(int[] arr1, int[] arr2) {// function to check arrays
  89.         int[] newArr = new int[5];// new array for result
  90.         int index = 0;// index for the new array
  91.  
  92.         for (int i = 0; i < arr1.length; i += 1) {// loop for the first array
  93.             for (int j = 0; j < arr2.length; j += 1) {// loop for the second array
  94.                 if (arr1[i] == arr2[j]) {// check to see if we have matched numbers in the two arrays
  95.                     newArr[index] = arr1[i];// saving the matched arrays to the new array
  96.                     index += 1;// incremnting the new array index
  97.                 }
  98.             }
  99.         }
  100.  
  101.         return newArr;
  102.     }
  103.  
  104. }
Advertisement
Add Comment
Please, Sign In to add comment