Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class funchw1 {
- public static void main(String[] args) {
- int dig = 0, num = 0, res = 0;
- int[] arr = new int[5];
- Scanner s = new Scanner(System.in);
- System.out.println("Please enter a number:");
- num = s.nextInt();// getting a number from user
- System.out.println("Please enter a single number between 1-9:");
- dig = s.nextInt();// getting a single number from user
- res = countDig(num, dig);// calling function
- System.out.printf("The number of time %d was found is %d\n\n", dig, res);// result
- System.out.println("Please enter a bunch of numbers:");
- for (int i = 0; i < arr.length; i += 1) {
- arr[i] = s.nextInt();// getting a bunch of numbers from user
- }
- System.out.println("Please enter a single number between 1-9:");
- dig = s.nextInt();// getting a single number from user
- res = countDig2(dig, arr);// calling function
- System.out.printf("The number of time %d was found is %d", dig, res);// result
- }
- public static int countDig(int num, int dig) {
- int count = 0;
- while (num > 0) {// as long as number is not 0
- if (num % 10 == dig) {// checking if the rightest number is the same as the digit
- count += 1;// count the time we found the a match
- }
- num /= 10;// removing the rightest number
- }
- return count;// returning count to main
- }
- public static int countDig2(int dig, int[] arr) {
- int count = 0, d = 0;
- for (int i = 0; i < arr.length; i += 1) {
- while (arr[i] > 0) {// checking the number from the array
- d = arr[i];// saving the number from the array
- if (d % 10 == dig) {// checking if the rightest number is the same as the digit
- count += 1;// count the time we found the a match
- }
- arr[i] /= 10;// removing the rightest number
- }
- }
- return count;// returning count to main
- }
- }
- --------------------------------------------------------------------------------------------------------------
- public class funchw2 {
- public static void main(String[] args) {
- int[] arr1 = new int[5];
- int[] arr2 = new int[5];
- int[] res = new int[5];
- Scanner s = new Scanner(System.in);
- System.out.println("Please enter a bunch of numbers to the first array:");
- for (int i = 0; i < arr1.length; i += 1) {
- arr1[i] = s.nextInt();// getting a bunch of numbers from user
- }
- System.out.println("Please enter a bunch of numbers to the second array:");
- for (int i = 0; i < arr2.length; i += 1) {
- arr2[i] = s.nextInt();// getting a bunch of numbers from user
- }
- res = createArr(arr1, arr2);// calling the function
- for (int i = 0; i < res.length; i += 1) {
- if (res[i] != 0) {// check if there are 0 in the array
- System.out.println(res[i]);// result
- }
- }
- }
- public static int[] createArr(int[] arr1, int[] arr2) {// function to check arrays
- int[] newArr = new int[5];// new array for result
- int index = 0;// index for the new array
- for (int i = 0; i < arr1.length; i += 1) {// loop for the first array
- for (int j = 0; j < arr2.length; j += 1) {// loop for the second array
- if (arr1[i] == arr2[j]) {// check to see if we have matched numbers in the two arrays
- newArr[index] = arr1[i];// saving the matched arrays to the new array
- index += 1;// incremnting the new array index
- }
- }
- }
- return newArr;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment