eng
competition

Text Practice Mode

coding in javaa

created Oct 27th, 19:30 by IsmailSalama


2


Rating

167 words
2 completed
00:00
public class Example {
     
    // Method to find the sum of an array of integers
    public static int sumOfArray(int[] numbers) {
        int sum = 0;   // Initialize sum
         
        // Iterate through the array
        for (int i = 0; i < numbers.length; i++) {
            sum += numbers[i];    // Add each element to sum
        }
         
        return sum;   // Return the sum
    }
 
    // Method to print a 2D array
    public static void print2DArray(int[][] array) {
        System.out.println("2D Array Elements:");
         
        for (int i = 0; i < array.length; i++) {
            for (int j = 0; j < array[i].length; j++) {
                System.out.print(array[i][j] + "\t");  // Tabbed output
            }
            System.out.println();    // New line after each row
        }
    }
 
    public static void main(String[] args) {
        int[] numbers = {1, 2, 3, 4, 5};   // Array of numbers
        int sum = sumOfArray(numbers);   // Call sumOfArray method
         
        System.out.println("Sum of Array: " + sum);    // Print sum
         
        int[][] matrix = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
        print2DArray(matrix);   // Call print2DArray method
    }
}
 

saving score / loading statistics ...