fa
Feedback
Tech Jargon - Decoded

Tech Jargon - Decoded

رفتن به کانال در Telegram

Confused by tech terms? Don’t worry, we’ve got you 🤝 We make things simple, one concept at a time. Learn daily Easy & clear Turn Confusion into clarity. #tech #it #softwareengineer #cs #development

نمایش بیشتر
2 018
مشترکین
اطلاعاتی وجود ندارد24 ساعت
-77 روز
-4030 روز
آرشیو پست ها
💻 Find Median of Array
import java.util.Arrays;
import java.util.Scanner;

public class FindMedianOfArray {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter the size of the array: ");
        int size = scanner.nextInt();

        int[] arr = new int[size];

        System.out.println("Enter the elements of the array:");
        for (int i = 0; i < size; i++) {
            arr[i] = scanner.nextInt();
        }

        Arrays.sort(arr); // Sort the array to easily find the median

        double median;
        if (size % 2 == 0) { // If the array size is even
            // Find the middle two elements and take their average
            int mid1 = arr[size / 2 - 1];
            int mid2 = arr[size / 2];
            median = (double) (mid1 + mid2) / 2;
        } else { // If the array size is odd
            // The median is the middle element
            median = arr[size / 2];
        }

        System.out.println("The median of the array is: " + median);

        scanner.close();
    }
}
📤 Output:
Input: 5
Input: 1
Input: 3
Input: 2
Input: 4
Input: 5
Output: The median of the array is: 3.0

Input: 6
Input: 1
Input: 2
Input: 3
Input: 4
Input: 5
Input: 6
Output: The median of the array is: 3.5

💻 Find Product of All Elements in Array
import java.util.Scanner;

public class ProductOfArray {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter the size of the array: ");
        int arraySize = scanner.nextInt();

        int[] myArray = new int[arraySize];

        System.out.println("Enter the elements of the array:");
        for (int i = 0; i < arraySize; i++) {
            myArray[i] = scanner.nextInt();
        }

        long product = 1; // Using long to avoid potential integer overflow

        for (int i = 0; i < arraySize; i++) {
            product *= myArray[i];
        }

        System.out.println("Product of all elements in the array: " + product);

        scanner.close();
    }
}
📤 Output:
Input: 3
Input: 1
Input: 2
Input: 3
Output: Enter the size of the array: Enter the elements of the array:
Product of all elements in the array: 6

Input: 4
Input: 1
Input: 2
Input: 3
Input: 4
Output: Enter the size of the array: Enter the elements of the array:
Product of all elements in the array: 24

Input: 2
Input: 5
Input: 10
Output: Enter the size of the array: Enter the elements of the array:
Product of all elements in the array: 50

Input: 5
Input: 1
Input: -2
Input: 3
Input: -4
Input: 5
Output: Enter the size of the array: Enter the elements of the array:
Product of all elements in the array: 120

💻 Find Cumulative Sum of Array
import java.util.Scanner;

public class CumulativeSumArray {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter the size of the array: ");
        int arraySize = scanner.nextInt();

        int[] inputArray = new int[arraySize];

        System.out.println("Enter the elements of the array:");
        for (int i = 0; i < arraySize; i++) {
            inputArray[i] = scanner.nextInt();
        }

        int[] cumulativeSumArray = new int[arraySize];
        cumulativeSumArray[0] = inputArray[0];

        for (int i = 1; i < arraySize; i++) {
            cumulativeSumArray[i] = cumulativeSumArray[i - 1] + inputArray[i];
        }

        System.out.println("Cumulative sum of the array:");
        for (int i = 0; i < arraySize; i++) {
            System.out.print(cumulativeSumArray[i] + " ");
        }
        System.out.println();

        scanner.close();
    }
}
📤 Output:
Input: 5
Input: 1
Input: 2
Input: 3
Input: 4
Input: 5
Output: Enter the size of the array: Enter the elements of the array:
Cumulative sum of the array:
1 3 6 10 15

💻 Count Positive and Negative Numbers in Array
import java.util.Scanner;

public class CountPositiveNegative {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter the number of elements in the array: ");
        int n = scanner.nextInt();

        int[] numbers = new int[n];

        System.out.println("Enter the elements of the array:");
        for (int i = 0; i < n; i++) {
            numbers[i] = scanner.nextInt();
        }

        int positiveCount = 0;
        int negativeCount = 0;

        for (int number : numbers) {
            if (number > 0) {
                positiveCount++;
            } else if (number < 0) {
                negativeCount++;
            }
        }

        System.out.println("Positive numbers count: " + positiveCount);
        System.out.println("Negative numbers count: " + negativeCount);

        scanner.close();
    }
}
📤 Output:
Input: 5
Input: 1
Input: -2
Input: 3
Input: -4
Input: 0
Output: Enter the number of elements in the array: Enter the elements of the array:
Positive numbers count: 2
Negative numbers count: 2

💻 Count Even and Odd Numbers in Array
import java.util.Scanner;

public class CountEvenOdd {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter the number of elements in the array: ");
        int numberOfElements = scanner.nextInt();

        int[] myArray = new int[numberOfElements];

        System.out.println("Enter the elements of the array:");
        for (int i = 0; i < numberOfElements; i++) {
            myArray[i] = scanner.nextInt();
        }

        int evenCount = 0;
        int oddCount = 0;

        for (int num : myArray) {
            if (num % 2 == 0) {
                evenCount++;
            } else {
                oddCount++;
            }
        }

        System.out.println("Even numbers: " + evenCount);
        System.out.println("Odd numbers: " + oddCount);

        scanner.close();
    }
}
📤 Output:
Input: 5
Input: 1
Input: 2
Input: 3
Input: 4
Input: 5
Output: Enter the number of elements in the array: Enter the elements of the array:
Even numbers: 2
Odd numbers: 3

💻 Find Second Smallest Element in Array
import java.util.Arrays;
import java.util.Scanner;

public class SecondSmallestElement {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter the size of the array: ");
        int arraySize = scanner.nextInt();

        int[] numbers = new int[arraySize];

        System.out.println("Enter the elements of the array:");
        for (int i = 0; i < arraySize; i++) {
            numbers[i] = scanner.nextInt();
        }

        if (arraySize < 2) {
            System.out.println("Array must have at least two elements.");
        } else {
            Arrays.sort(numbers); // Sort the array

            System.out.println("Second smallest element: " + numbers[1]);
        }

        scanner.close();
    }
}
📤 Output:
Input: 5
Input: 10
Input: 5
Input: 8
Input: 2
Input: 7
Output: Enter the size of the array: Enter the elements of the array:
Second smallest element: 5

Input: 1
Input: 5
Output: Enter the size of the array: Enter the elements of the array:
Array must have at least two elements.

💻 Find Second Largest Element in Array
import java.util.Scanner;

public class SecondLargestInArray {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter the size of the array: ");
        int size = scanner.nextInt();

        int[] arr = new int[size];

        System.out.println("Enter the elements of the array:");
        for (int i = 0; i < size; i++) {
            arr[i] = scanner.nextInt();
        }

        int largest = Integer.MIN_VALUE;
        int secondLargest = Integer.MIN_VALUE;

        for (int num : arr) {
            if (num > largest) {
                secondLargest = largest;
                largest = num;
            } else if (num > secondLargest && num != largest) {
                secondLargest = num;
            }
        }

        if (secondLargest == Integer.MIN_VALUE) {
            System.out.println("There is no second largest element in the array.");
        } else {
            System.out.println("The second largest element in the array is: " + secondLargest);
        }

        scanner.close();
    }
}
📤 Output:
Input: 5
Input: 10
Input: 5
Input: 8
Input: 2
Input: 7
Output: The second largest element in the array is: 8

Input: 3
Input: 1
Input: 1
Input: 1
Output: There is no second largest element in the array.

Input: 2
Input: 5
Input: 10
Output: The second largest element in the array is: 5

💻 Remove Duplicates from Array
import java.util.Scanner;
import java.util.Arrays;

public class RemoveDuplicatesFromArray {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter the size of the array: ");
        int arraySize = scanner.nextInt();

        int[] inputArray = new int[arraySize];

        System.out.println("Enter the elements of the array:");
        for (int i = 0; i < arraySize; i++) {
            inputArray[i] = scanner.nextInt();
        }

        int[] uniqueArray = removeDuplicates(inputArray);

        System.out.print("Array after removing duplicates: ");
        for (int i = 0; i < uniqueArray.length; i++) {
            System.out.print(uniqueArray[i] + " ");
        }
        System.out.println();

        scanner.close();
    }

    public static int[] removeDuplicates(int[] inputArray) {
        Arrays.sort(inputArray); // Sort the array to easily find duplicates

        if (inputArray.length == 0) {
            return inputArray;
        }

        int j = 0;
        for (int i = 0; i < inputArray.length - 1; i++) {
            if (inputArray[i] != inputArray[i + 1]) {
                inputArray[j++] = inputArray[i];
            }
        }
        inputArray[j++] = inputArray[inputArray.length - 1]; // Add the last element

        int[] uniqueArray = new int[j];
        System.arraycopy(inputArray, 0, uniqueArray, 0, j); // Copy unique elements to a new array

        return uniqueArray;
    }
}
📤 Output:
Input: 5
Input: 1
Input: 2
Input: 2
Input: 3
Input: 4
Output: Enter the size of the array: Enter the elements of the array:
Array after removing duplicates: 1 2 3 4
Input: 7
Input: 5
Input: 2
Input: 2
Input: 1
Input: 5
Input: 3
Input: 1
Output: Enter the size of the array: Enter the elements of the array:
Array after removing duplicates: 1 2 3 5
Input: 3
Input: 1
Input: 1
Input: 1
Output: Enter the size of the array: Enter the elements of the array:
Array after removing duplicates: 1

💻 Check if Array is Sorted
import java.util.Scanner;

public class ArraySortedChecker {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter the size of the array: ");
        int size = scanner.nextInt();

        int[] array = new int[size];

        System.out.println("Enter the elements of the array:");
        for (int i = 0; i < size; i++) {
            array[i] = scanner.nextInt();
        }

        boolean isSorted = true;
        for (int i = 0; i < size - 1; i++) {
            if (array[i] > array[i + 1]) {
                isSorted = false;
                break;
            }
        }

        if (isSorted) {
            System.out.println("The array is sorted.");
        } else {
            System.out.println("The array is not sorted.");
        }

        scanner.close();
    }
}
📤 Output:
Input: 5
Input: 1
Input: 2
Input: 3
Input: 4
Input: 5
Output: The array is sorted.

Input: 4
Input: 5
Input: 2
Input: 8
Input: 1
Output: The array is not sorted.

Input: 3
Input: 10
Input: 20
Input: 15
Output: The array is not sorted.

💻 Reverse an Array
import java.util.Scanner;

public class ReverseArray {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter the size of the array: ");
        int arraySize = scanner.nextInt();

        int[] myArray = new int[arraySize];

        System.out.println("Enter the elements of the array:");
        for (int i = 0; i < arraySize; i++) {
            myArray[i] = scanner.nextInt();
        }

        // Reverse the array
        int start = 0;
        int end = arraySize - 1;

        while (start < end) {
            // Swap elements at start and end
            int temp = myArray[start];
            myArray[start] = myArray[end];
            myArray[end] = temp;

            // Move indices towards the middle
            start++;
            end--;
        }

        System.out.println("Reversed array:");
        for (int i = 0; i < arraySize; i++) {
            System.out.print(myArray[i] + " ");
        }
        System.out.println();

        scanner.close();
    }
}
📤 Output:
Input: 5
Input: 1
Input: 2
Input: 3
Input: 4
Input: 5
Output: Enter the size of the array: Enter the elements of the array:
Reversed array:
5 4 3 2 1

💻 Average of Array Elements
import java.util.Scanner;

public class AverageOfArrayElements {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter the number of elements in the array: ");
        int numberOfElements = scanner.nextInt();

        int[] myArray = new int[numberOfElements];
        double sum = 0;

        System.out.println("Enter the elements of the array:");
        for (int i = 0; i < numberOfElements; i++) {
            myArray[i] = scanner.nextInt();
            sum += myArray[i];
        }

        double average = sum / numberOfElements;

        System.out.println("The average of the array elements is: " + average);

        scanner.close();
    }
}
📤 Output:
Input: 5
Input: 1
Input: 2
Input: 3
Input: 4
Input: 5
Output: The average of the array elements is: 3.0

💻 Sum of All Elements in Array
import java.util.Scanner;

public class SumOfArrayElements {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter the number of elements in the array: ");
        int numElements = scanner.nextInt();

        int[] myArray = new int[numElements];
        int sum = 0;

        System.out.println("Enter the elements of the array:");

        for (int i = 0; i < numElements; i++) {
            myArray[i] = scanner.nextInt();
            sum += myArray[i];
        }

        System.out.println("Sum of all elements in the array: " + sum);

        scanner.close();
    }
}
📤 Output:
Input: 5
Input: 1
Input: 2
Input: 3
Input: 4
Input: 5
Output: Enter the number of elements in the array: Enter the elements of the array:
Sum of all elements in the array: 15

💻 Find Smallest Element in an Array
import java.util.Scanner;

public class FindSmallestElement {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter the number of elements in the array: ");
        int numberOfElements = scanner.nextInt();

        int[] myArray = new int[numberOfElements];

        System.out.println("Enter the elements of the array:");
        for (int i = 0; i < numberOfElements; i++) {
            myArray[i] = scanner.nextInt();
        }

        int smallestElement = myArray[0];
        for (int i = 1; i < numberOfElements; i++) {
            if (myArray[i] < smallestElement) {
                smallestElement = myArray[i];
            }
        }

        System.out.println("The smallest element in the array is: " + smallestElement);
        scanner.close();
    }
}
📤 Output:
Input: 5
Input: 10
Input: 5
Input: 8
Input: 2
Input: 15
Output: Enter the number of elements in the array: Enter the elements of the array:
The smallest element in the array is: 2

💻 Find Largest Element in an Array
import java.util.Scanner;

public class LargestElementInArray {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter the number of elements in the array: ");
        int numberOfElements = scanner.nextInt();

        int[] myArray = new int[numberOfElements];

        System.out.println("Enter the elements of the array:");
        for (int i = 0; i < numberOfElements; i++) {
            myArray[i] = scanner.nextInt();
        }

        int largestElement = myArray[0];
        for (int i = 1; i < numberOfElements; i++) {
            if (myArray[i] > largestElement) {
                largestElement = myArray[i];
            }
        }

        System.out.println("The largest element in the array is: " + largestElement);

        scanner.close();
    }
}
📤 Output:
Input: 5
Input: 10
Input: 5
Input: 20
Input: 8
Input: 15
Output: Enter the number of elements in the array: Enter the elements of the array:
The largest element in the array is: 20

Arrays - 1D

💻 Static Method Implementation
import java.util.Scanner;

public class StaticMethodExample {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter the first number: ");
        int num1 = scanner.nextInt();

        System.out.print("Enter the second number: ");
        int num2 = scanner.nextInt();

        int sum = addNumbers(num1, num2);

        System.out.println("The sum is: " + sum);

        scanner.close();
    }

    static int addNumbers(int a, int b) {
        return a + b;
    }
}
📤 Output:
Input: 5
Input: 10
Output: Enter the first number: Enter the second number: The sum is: 15

💻 Method with Return Type
import java.util.Scanner;

public class MethodWithReturnType {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter the first number: ");
        int firstNumber = scanner.nextInt();

        System.out.print("Enter the second number: ");
        int secondNumber = scanner.nextInt();

        int sum = calculateSum(firstNumber, secondNumber);

        System.out.println("The sum of the two numbers is: " + sum);

        scanner.close();
    }

    // Method to calculate the sum of two integers and return the result
    public static int calculateSum(int num1, int num2) {
        int sum = num1 + num2;
        return sum; // Returning the calculated sum
    }
}
📤 Output:
Input: 10
Input: 20
Output: The sum of the two numbers is: 30

💻 Recursive Method to Find Sum of Natural Numbers
import java.util.Scanner;

public class RecursiveSum {

    public static int sumOfNaturalNumbers(int n) {
        if (n <= 0) {
            return 0;
        } else {
            return n + sumOfNaturalNumbers(n - 1); // Recursive call
        }
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter a natural number: ");
        int number = scanner.nextInt();

        int sum = sumOfNaturalNumbers(number);
        System.out.println("Sum of natural numbers up to " + number + " is: " + sum);

        scanner.close();
    }
}
📤 Output:
Input: 5
Output: Enter a natural number: Sum of natural numbers up to 5 is: 15

Input: 10
Output: Enter a natural number: Sum of natural numbers up to 10 is: 55

Input: 0
Output: Enter a natural number: Sum of natural numbers up to 0 is: 0

Input: 1
Output: Enter a natural number: Sum of natural numbers up to 1 is: 1

💻 Varargs Method Demonstration
import java.util.Scanner;

public class VarargsMethodDemo {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter some numbers separated by space: ");
        String input = scanner.nextLine();
        String[] numberStrings = input.split(" ");

        int[] numbers = new int[numberStrings.length];
        for (int i = 0; i < numberStrings.length; i++) {
            numbers[i] = Integer.parseInt(numberStrings[i]);
        }

        int sum = calculateSum(numbers);
        System.out.println("Sum of the numbers: " + sum);

        scanner.close();
    }

    // Method using varargs to calculate the sum of numbers
    public static int calculateSum(int... numbers) {
        int sum = 0;
        for (int number : numbers) {
            sum += number;
        }
        return sum;
    }
}
📤 Output:
Input: 1 2 3 4 5
Output: Enter some numbers separated by space: Sum of the numbers: 15

💻 Method Overloading Examples
import java.util.Scanner;

public class MethodOverloadingExamples {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.println("Example of Method Overloading");

        // Creating an object of the class
        MethodOverloadingExamples obj = new MethodOverloadingExamples();

        System.out.println("Sum of 2 integers: " + obj.add(5, 10));
        System.out.println("Sum of 3 integers: " + obj.add(5, 10, 15));
        System.out.println("Sum of 2 doubles: " + obj.add(2.5, 3.5));

        scanner.close();
    }

    // Method to add two integers
    public int add(int num1, int num2) {
        return num1 + num2;
    }

    // Method to add three integers
    public int add(int num1, int num2, int num3) {
        return num1 + num2 + num3;
    }

    // Method to add two doubles
    public double add(double num1, double num2) {
        return num1 + num2;
    }
}
📤 Output:
Example of Method Overloading
Sum of 2 integers: 15
Sum of 3 integers: 30
Sum of 2 doubles: 6.0