-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathElementSwap.java
More file actions
48 lines (45 loc) · 1.25 KB
/
ElementSwap.java
File metadata and controls
48 lines (45 loc) · 1.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
// Program: Replace Negative Elements in a 2D Array with Zero
// Topic: 2D Arrays and Conditional Logic
// Description: Reads a 2x2 integer matrix from user input, replaces all negative elements with zero,
// and prints the modified matrix. Demonstrates nested loops and conditional element updates.
package Array2D;
import java.util.*;
/**
*
* @author Samim
*/
public class ElementSwap
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int ar[][]=new int [2][2];
for(int i=0;i<ar.length;i++)
{
for(int j=0;j<ar.length;j++)
{
ar[i][j]=sc.nextInt();
}
}
//Replacing (-)ve Element with 0
for(int i=0;i<ar.length;i++)
{
for(int j=0;j<ar.length;j++)
{
if(ar[i][j]<0)
{
ar[i][j]=0;
}
}
}
//PRinting New Array
for(int i=0;i<ar.length;i++)
{
for(int j=0;j<ar.length;j++)
{
System.out.print(ar[i][j]+" ");
}
System.out.println();
}
}
}