Program on address calculation in Two Dimensional array
import java.util.*;
class tda
{
public static void main(String args[])
{
int item,m,n;
Scanner s = new Scanner (System.in);
System.out.print( " Enter the size of the row : ");
m=s.nextInt();
System.out.print( " Enter the size of the col : ");
n=s.nextInt();
//int[] a; // declaration
//a=new int[n];// creation
int[][] a= new int[m][n]; // declaration and creation
System.out.printf("\n Enter %d Elements ",m*n);
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
System.out.printf("\n Elements a[%d][%d] =",i,j);
a[i][j]=s.nextInt();
}
}
System.out.print("\n Array elements : ");
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
System.out.print( a[i][j] + " ");
}
}
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
System.out.printf("\nArray = a[%d][%d] ---- Data = %d",i,j,a[i][j]);
}
}
System.out.print("\n Enter the Array element which u want : ");
item=s.nextInt();
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
if(item == a[i][j])
{
System.out.printf("\nArray = a[%d][%d] ---- Data = %d",i,j,a[i][j]);
}
}
}
}
}
output:
import java.util.*;
class tda
{
public static void main(String args[])
{
int item,m,n;
Scanner s = new Scanner (System.in);
System.out.print( " Enter the size of the row : ");
m=s.nextInt();
System.out.print( " Enter the size of the col : ");
n=s.nextInt();
//int[] a; // declaration
//a=new int[n];// creation
int[][] a= new int[m][n]; // declaration and creation
System.out.printf("\n Enter %d Elements ",m*n);
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
System.out.printf("\n Elements a[%d][%d] =",i,j);
a[i][j]=s.nextInt();
}
}
System.out.print("\n Array elements : ");
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
System.out.print( a[i][j] + " ");
}
}
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
System.out.printf("\nArray = a[%d][%d] ---- Data = %d",i,j,a[i][j]);
}
}
System.out.print("\n Enter the Array element which u want : ");
item=s.nextInt();
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
if(item == a[i][j])
{
System.out.printf("\nArray = a[%d][%d] ---- Data = %d",i,j,a[i][j]);
}
}
}
}
}
output:
No comments:
Post a Comment