-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSkipAlternateElement.java
More file actions
55 lines (43 loc) · 1.02 KB
/
SkipAlternateElement.java
File metadata and controls
55 lines (43 loc) · 1.02 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
49
50
51
52
53
54
55
package matrices;
import java.util.Scanner;
public class SkipAlternateElement {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int m,n;
System.out.println("Enter the row and column Size : ");
m = sc.nextInt();
n = sc.nextInt();
int[][] a = new int[m][n];
//read the matrix elements
System.out.println("Enter the Matrix Elements : ");
for(int i = 0;i<m;i++)
{
for(int j = 0;j<n;j++)
{
a[i][j] = sc.nextInt();
}
}
//lines to print alternate elements of an matrix
System.out.println("Matrix Elements Skipping Alternate Elements : ");
for(int i = 0;i<m;i++) {
if(i % 2 == 0)
for(int j = 0;j<n;j+=2)
System.out.print(a[i][j]+" ");
else
for(int j = 1;j<n;j+=2)
System.out.print(a[i][j]+" ");
}
}
}
/*
Output :
Enter the row and column Size :
4 3
Enter the Matrix Elements :
1 2 3
4 5 6
7 8 9
8 5 2
Matrix Elements Skipping Alternate Elements :
1 3 5 7 9 5
*/