Skip to content

Commit 9711ae9

Browse files
authored
Add files via upload
1 parent 4b3b93c commit 9711ae9

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

JavaArray08.txt

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*Write a Java program that will take the number of rows and columns from the user and create a 2D array by taking integer numbers from the user. Print the 2D array. Finally, create a 1D array by flattening the 2D array.
2+
3+
Sample Input Sample Output
4+
row = 2 2D Array:
5+
column = 3 1 2 3
6+
1 4 5 6
7+
2
8+
3 1D Array:
9+
4 1 2 3 4 5 6
10+
5
11+
6
12+
*/
13+
14+
import java.util.Scanner;
15+
public class Task08{
16+
public static void main(String [] args){
17+
Scanner sc= new Scanner(System.in);
18+
int row= sc.nextInt();
19+
int col= sc.nextInt();
20+
int k=0;
21+
int [] [] arr = new int [row] [col];
22+
int [] arr1 = new int [row*col];
23+
for(int i=0;i<arr.length;i++){
24+
for(int j=0;j<col;j++){
25+
arr[i][j]=sc.nextInt();}}
26+
System.out.println("2D Array: ");
27+
for(int i=0;i<arr.length;i++){
28+
for(int j=0;j<col;j++){
29+
System.out.print(arr[i][j] + " ");}
30+
System.out.println(" ");}
31+
for(int i=0;i<arr.length;i++){
32+
for(int j=0;j<col;j++){
33+
arr1[k++]=arr[i][j];}}
34+
System.out.println("1D Array: ");
35+
for(int j=0;j<arr1.length;j++){
36+
System.out.print(arr1[j] + " ");}
37+
}
38+
}
39+

0 commit comments

Comments
 (0)