0% found this document useful (0 votes)
70 views

Array 2D Programs

The documents demonstrate how to create and manipulate 2D arrays in Java. Methods shown include initializing a 2D array, inputting values, printing array elements, calculating sums and products of rows and columns, and using a Scanner to input array size and values.

Uploaded by

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

Array 2D Programs

The documents demonstrate how to create and manipulate 2D arrays in Java. Methods shown include initializing a 2D array, inputting values, printing array elements, calculating sums and products of rows and columns, and using a Scanner to input array size and values.

Uploaded by

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

Display the elements of the 2D array of 4x3

public class array_2d


The elements of the 2D array are
{
1 2 3
public static void main()
2 4 6
{
3 4 7
int data[][]={{1,2,3},{2,4,6},{3,4,7},{5,6,7}};
5 6 7
System.out.println("The elements of the 2D array are ");
for(int i=0;i<4;i++)
{
for(int j=0;j<3;j++)
{
System.out.print(data[i][j]+"\t");
}
System.out.println();
}
}
}

Display the elements of the 2D array of 2x3 using Scanner class


import java.util.*;
Enter the rows
public class array2D_Scanner
2
{
Enter the columns
public static void main()
3
{
Enter the values for 2D array
Scanner sc=new Scanner(System.in);
1
int i,j,rows,columns;
Enter the values for 2D array
System.out.println("Enter the rows");
2
rows=sc.nextInt();
Enter the values for 2D array
System.out.println("Enter the columns");
3
columns=sc.nextInt();
Enter the values for 2D array
int a[][]=new int[rows][columns];
4
for(i=0;i<rows;i++)
Enter the values for 2D array
{
5
for(j=0;j<columns;j++)
Enter the values for 2D array
{
6
System.out.println("Enter the values for 2D array");
123
a[i][j]=sc.nextInt();
456
}
}
for(i=0;i<rows;i++)
{
for(j=0;j<columns;j++)
{
System.out.print(a[i][j]+" ");
}
System.out.println();
}
}
}
Create a 2D array of size 2x3 and print square of all the odd numbers in the array
import java.util.*;
enter the numbers
public class array_2D_oddsum
1
{
enter the numbers
public static void main()
2
{
enter the numbers
Scanner sc=new Scanner(System.in);
3
int a[][]=new int[2][3];
enter the numbers
int i, j;
4
for(i=0;i<2;i++)
enter the numbers
{
5
for(j=0;j<3;j++)
enter the numbers
{
6
System.out.println("enter the numbers");
odd number :1 and its square is : 1.0
a[i][j]=sc.nextInt();
odd number :3 and its square is : 9.0
}
odd number :5 and its square is : 25.0
}
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
if(a[i][j]%2!=0)
System.out.println("odd number :"+a[i][j]+" and its square is : "+Math.pow(a[i][j],2));
}
}
}
}

WAP to input elements in a 2D array of size mxn and print the sum of all the number row wise and product of
all the elements column wise.
import java.util.*;
public class array2D_rowsum_colpro Enter the rows
{ 2
public static void main() Enter the columns
{ 2
Scanner sc=new Scanner(System.in); Enter the values for 2D array
int i,j,rows,columns,sum=0,pro=1; 1
System.out.println("Enter the rows"); Enter the values for 2D array
rows=sc.nextInt(); 2
System.out.println("Enter the columns"); Enter the values for 2D array
columns=sc.nextInt();
3
int a[][]=new int[rows][columns];
Enter the values for 2D array
for(i=0;i<rows;i++)
{ 4
for(j=0;j<columns;j++) Sum of rows : 10
{ Product of columns : 24
System.out.println("Enter the values for 2D array");
a[i][j]=sc.nextInt();
}
}
for(i=0;i<rows;i++)
{
for(j=0;j<columns;j++)
{

sum=sum+a[i][j];
pro=pro*a[j][i];
}}
System.out.println("Sum of rows : "+sum);

System.out.println("Product of columns : "+pro);


}
}

WAP to input elements in a 2D array of size mxn and print the sum of all the number row wise.
import java.util.*;
public class array2D_each_rowsum Enter the rows
{ 2
public static void main() Enter the columns
{ 2
Scanner sc=new Scanner(System.in); Enter the values for 2D array
int i,j,rows,columns,sum; 1
System.out.println("Enter the rows"); Enter the values for 2D array
rows=sc.nextInt(); 2
System.out.println("Enter the columns");
Enter the values for 2D array
columns=sc.nextInt();
3
int a[][]=new int[rows][columns];
Enter the values for 2D array
for(i=0;i<rows;i++)
{ 4
for(j=0;j<columns;j++) Sum of rows 1 : 3
{ Sum of rows 2 : 7
System.out.println("Enter the values for 2D array");
a[i][j]=sc.nextInt();
}
}

for(i=0;i<rows;i++)
{
sum=0;
for(j=0;j<columns;j++)
{
sum=sum+a[i][j];
}
System.out.println("Sum of rows "+(i+1)+" : "+sum);
} }
}

WAP to input elements in a 2D array of size mxn and print the sum of all the number column wise.
import java.util.*; Enter the rows
public class array2D_each_colsum 2
{
Enter the columns
public static void main()
2
{
Scanner sc=new Scanner(System.in); Enter the values for 2D array
int i,j,rows,columns,sum; 1
System.out.println("Enter the rows"); Enter the values for 2D array
rows=sc.nextInt(); 2
System.out.println("Enter the columns"); Enter the values for 2D array
columns=sc.nextInt(); 3
int a[][]=new int[rows][columns]; Enter the values for 2D array
for(i=0;i<rows;i++) 4
{ Sum of columns 1 : 4
for(j=0;j<columns;j++) Sum of columns 2 : 6
{
System.out.println("Enter the values for 2D array");
a[i][j]=sc.nextInt();
}
}

for(j=0;j<columns;j++)
{
sum=0;
for(i=0;i<rows;i++)
{
sum=sum+a[i][j];

}
System.out.println("Sum of columns "+(j+1)+" :
"+sum);
} }
}

WAP to input elements in a 2D array of size mxn and print the product of all the number row wise.
import java.util.*;
public class array2D_each_rowproduct Enter the rows
{ 2
public static void main() Enter the columns
{ 2
Scanner sc=new Scanner(System.in); Enter the values for 2D array
int i,j,rows,columns,pro; 1
System.out.println("Enter the rows"); Enter the values for 2D array
rows=sc.nextInt(); 2
System.out.println("Enter the columns");
Enter the values for 2D array
columns=sc.nextInt();
3
int a[][]=new int[rows][columns];
for(i=0;i<rows;i++) Enter the values for 2D array
{ 4
for(j=0;j<columns;j++) Product of rows 1 : 2
{ Product of rows 2 : 12
System.out.println("Enter the values for 2D array");
a[i][j]=sc.nextInt();
}
}
for(i=0;i<rows;i++)
{
pro=1;
for(j=0;j<columns;j++)
{
pro=pro*a[i][j];

}
System.out.println("Product of rows "+(i+1)+" : "+pro);
} }
}

WAP to input elements in a 2D array of size mxn and print the product of all the number column wise.
import java.util.*; Enter the rows
public class array2D_each_colproduct 2
{ Enter the columns
public static void main() 2
{ Enter the values for 2D array
Scanner sc=new Scanner(System.in); 1
int i,j,rows,columns,pro; Enter the values for 2D array
System.out.println("Enter the rows"); 2
rows=sc.nextInt(); Enter the values for 2D array
System.out.println("Enter the columns"); 3
columns=sc.nextInt(); Enter the values for 2D array
int a[][]=new int[rows][columns]; 4
for(i=0;i<rows;i++) Product of columns 1 : 3
{ Product of columns 2 : 8
for(j=0;j<columns;j++)
{
System.out.println("Enter the values for 2D array");
a[i][j]=sc.nextInt();
}
}
for(j=0;j<columns;j++)
{
pro=1;
for(i=0;i<rows;i++)
{
pro=pro*a[i][j];

}
System.out.println("Product of columns "+(j+1)+" : "+pro);
} }
}

You might also like