Untitled

mail@pastecode.io avatar
unknown
plain_text
2 months ago
2.5 kB
7
Indexable
Never
import java.util.Arrays;

public class Main {
    public static void main(String[] args) {
        // Check if there are command line arguments
        if (args.length == 0) {
            System.out.println("No input provided.");
            return;
        }
        
        // Create an array to store the input values
        int[] array = new int[args.length];
        
        // Parse and store the input values
        for (int i = 0; i < args.length; i++) {
            try {
                array[i] = Integer.parseInt(args[i]);
            } catch (NumberFormatException e) {
                System.out.println("Invalid input: " + args[i]);
                return;
            }
        }
        
        // Sort the array
        Arrays.sort(array);
        
        // Display the sorted array
        System.out.println("Sorted array:");
        for (int num : array) {
            System.out.print(num + " ");
        }
    }
}


public class MatrixSum {
    public static void main(String[] args) {
        // Define matrices
        int[][] matrix1 = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
        int[][] matrix2 = {{9, 8, 7}, {6, 5, 4}, {3, 2, 1}};
        int[][] sumMatrix = new int[3][3];

        // Find the sum of matrices
        for (int i = 0; i < matrix1.length; i++) {
            for (int j = 0; j < matrix1[0].length; j++) {
                sumMatrix[i][j] = matrix1[i][j] + matrix2[i][j];
            }
        }

        // Display matrices
        System.out.println("Matrix 1:");
        displayMatrix(matrix1);

        System.out.println("Matrix 2:");
        displayMatrix(matrix2);

        System.out.println("Sum Matrix:");
        displayMatrix(sumMatrix);
    }

    // Function to display matrix
    public static void displayMatrix(int[][] matrix) {
        for (int[] row : matrix) {
            for (int element : row) {
                System.out.print(element + " ");
            }
            System.out.println();
        }
        System.out.println();
    }
}


import java.util.Scanner;

public class HarmonicSeriesSum {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter the value of n: ");
        int n = scanner.nextInt();
        
        double sum = 0.0;
        for (int i = 1; i <= n; i++) {
            sum += 1.0 / i;
        }
        
        System.out.println("Sum of the harmonic series for n = " + n + " is: " + sum);
    }
}
Leave a Comment