File tree Expand file tree Collapse file tree 1 file changed +64
-0
lines changed
Codechef Problems/DSA Learning Series Expand file tree Collapse file tree 1 file changed +64
-0
lines changed Original file line number Diff line number Diff line change
1
+ /* You're given a number N. Print the first N lines of the below-given pattern.
2
+
3
+ 1 2 3 4 5
4
+ 10 9 8 7 6
5
+ 11 12 13 14 15
6
+ 20 19 18 17 16
7
+ 21 22 23 24 25
8
+ 30 29 28 27 26
9
+ Input:
10
+ First-line will contain the number N.
11
+ Output:
12
+ Print the first N lines of the given pattern.
13
+
14
+ Constraints
15
+ 1≤N≤200
16
+ Sample Input 1:
17
+ 4
18
+ Sample Output 1:
19
+ 1 2 3 4 5
20
+ 10 9 8 7 6
21
+ 11 12 13 14 15
22
+ 20 19 18 17 16
23
+ Sample Input 2:
24
+ 2
25
+ Sample Output 2:
26
+ 1 2 3 4 5
27
+ 10 9 8 7 6
28
+ EXPLANATION:
29
+ In the first example, we'll print the first 4 lines of the given pattern.
30
+ In the second example, we'll print the first 2 lines of the given pattern. */
31
+
32
+ import java .util .*;
33
+ import java .lang .*;
34
+ import java .io .*;
35
+
36
+ /* Name of the class has to be "Main" only if the class is public. */
37
+ class Codechef
38
+ {
39
+ public static void main (String [] args ) throws java .lang .Exception
40
+ {
41
+ Scanner sc = new Scanner (System .in );
42
+ int n = sc .nextInt ();
43
+ for (int i =1 ; i <=n ; i ++)
44
+ {
45
+ if (i %2 !=0 )
46
+ {
47
+ for (int j =(5 *(i -1 )+1 );j <=(5 *i );j ++)
48
+ {
49
+ System .out .print (j +" " );
50
+ }
51
+ System .out .println ();
52
+ }
53
+ else {
54
+ for (int j =(5 *i );j >=(5 *(i -1 )+1 );j --)
55
+ {
56
+ System .out .print (j +" " );
57
+ }
58
+ System .out .println ();
59
+ }
60
+
61
+
62
+ }
63
+ }
64
+ }
You can’t perform that action at this time.
0 commit comments