- import java.util.Scanner;
- public class Main {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- // Input the size of the matrix
- int n = scanner.nextInt();
- // Input the row and column numbers of the element to be printed
- int row = scanner.nextInt();
- int col = scanner.nextInt();
- int[][] matrix = new int[n][n];
- // Direction flags: 0 for downwards, 1 for upwards
- int direction = 0;
- int num = 1;
- // Loop to fill the matrix
- for (int i = 0; i < n; i++) {
- // If direction is downwards
- if (direction == 0) {
- for (int j = 0; j < n; j++) {
- matrix[j][i] = num++;
- }
- direction = 1; // Change direction to upwards
- }
- // If direction is upwards
- else {
- for (int j = n - 1; j >= 0; j--) {
- matrix[j][i] = num++;
- }
- direction = 0; // Change direction to downwards
- }
- }
- // Output the matrix
- for (int i = 0; i < n; i++) {
- for (int j = 0; j < n; j++) {
- System.out.print(matrix[i][j] + " ");
- }
- System.out.println();
- }
- // Output the element at the given row and column
- System.out.println(matrix[row - 1][col - 1]);
- }
- }
[text] arrays
Viewer
*** This page was generated with the meta tag "noindex, nofollow". This happened because you selected this option before saving or the system detected it as spam. This means that this page will never get into the search engines and the search bot will not crawl it. There is nothing to worry about, you can still share it with anyone.
Editor
You can edit this paste and save as new: