0% found this document useful (0 votes)
7 views2 pages

Matrix

Uploaded by

hkschool999
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views2 pages

Matrix

Uploaded by

hkschool999
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

MATRIX

(2D ARRAYS)

//creating a 2D array syntax:


1. For Integer dataType:

int[][] matrix = new int[rows][cols];

2. For Decimal(float) dataType:

float[][] matrix = new float[rows][cols];

3. For Decimal(double) dataType:

double[][] matrix = new double[rows][cols];

/* take a matrix as input from the user.


Search for a given number x and print the indices at which it occurs.*/

import java.util.*;
public class Main{
public static void main(String args[]){
Scanner in = new Scanner(System.in);

System.out.println("Enter no. of rows: ");


int rows = in.nextInt();

System.out.println("Enter no. of columns: ");


int cols = in.nextInt();

int[][] numbers = new int[rows][cols];

//input
//rows
for(int i = 0; i < rows; i++){
//rows
for(int j = 0; j < cols; j++){
numbers[i][j] = in.nextInt();
}
}
System.out.println("Enter the number you want to
search: ");
int x = in.nextInt();

//output
for(int i = 0; i < rows; i++){
for(int j = 0; j < cols; j++){
if(numbers[i][j] == x){
System.out.println(x + " found at index ("
+ i + "," + j + ")" );
}
}
}
}
}

You might also like