Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class MatsegetFunctions {
- // targil 1.1
- public static int numberAppears(int current_num, int num_to_find ) {
- int counter = 0, temp_digit;
- while (current_num>0){
- temp_digit = current_num%10;
- if(temp_digit == num_to_find){
- counter+=1;
- }
- current_num/=10;
- }
- return counter;
- }
- //targil 1.2 בשביל לפתור חלק השני של תרגיל ראשון אפשר להעזר בפונקציה שכבר כתבנו בחלק ראשון של תרגיל הזה
- public static int digitInArray(int[] temp_array, int numb_to_find ) {
- int counter = 0;
- for (int i=0; i<temp_array.length; i+=1) {
- counter += numberAppears(temp_array[i], numb_to_find);
- }
- return counter;
- }
- //targil 2
- public static int [] sameChains(int [] first_arr, int [] second_arr) {
- int current_place = 0;
- int[] temp_arr = new int[first_arr.length]; //Declaration of array to return
- for (int i = 0; i < first_arr.length; i += 1) {
- for (int j = 0; j < second_arr.length; j += 1) {
- if (first_arr[i]==second_arr[j]){
- temp_arr[current_place] = first_arr[i];
- current_place+=1;
- }
- }
- }
- return temp_arr;
- }
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- //קליטת נתונים לתרגיל 1
- int big_num, digit;
- int [] arr1 = new int [10];
- System.out.println("Enter a big number and a digit: ");
- big_num=sc.nextInt(); // קליטת מספר גדול בשביל חלק ראשון של התרגיל
- digit=sc.nextInt(); // קליטת סיפרה בשביל חלק ראשון וגם כן חלק שני של התרגיל
- System.out.printf("The number %d appears %d times\n", digit, numberAppears(big_num,digit));
- System.out.printf("Enter %d numbers to array: \n",arr1.length);
- for (int i=0;i<arr1.length;i+=1){
- arr1[i] = sc.nextInt();
- }
- System.out.printf("This number %d appears %d times\n", digit, digitInArray(arr1,digit));
- //קליטת נתונים לתרגיל 2
- int [] result_arr , arr2 = new int [5], arr3 = new int [5];
- System.out.printf("Enter %d numbers to the first array : \n",arr2.length);
- for (int i=0; i<arr2.length; i+=1){
- arr2[i]=sc.nextInt();
- }
- System.out.printf("Enter %d numbers to the second array : \n",arr3.length);
- for (int i=0; i<arr3.length;i+=1){
- arr3[i]=sc.nextInt();
- }
- result_arr = sameChains(arr2, arr3); // שימוש בפונקציה
- for (int i:result_arr) {
- if (i!=0) {
- System.out.print(i + " ");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment