Skip to content

Commit 2b5bda0

Browse files
added solution
1 parent 5e29cd3 commit 2b5bda0

File tree

2 files changed

+57
-0
lines changed
  • CodeChef DSA Learning series Solutions/Easy Problems to get started/Alternative Square Pattern

2 files changed

+57
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
Alternative Square Pattern Problem Code: SQALPAT
2+
Add problem to Todo list
3+
4+
You're given a number N. Print the first N lines of the below-given pattern.
5+
6+
1 2 3 4 5
7+
10 9 8 7 6
8+
11 12 13 14 15
9+
20 19 18 17 16
10+
21 22 23 24 25
11+
30 29 28 27 26
12+
Input:
13+
First-line will contain the number N.
14+
Output:
15+
Print the first N lines of the given pattern.
16+
17+
Constraints
18+
1≤N≤200
19+
Sample Input 1:
20+
4
21+
Sample Output 1:
22+
1 2 3 4 5
23+
10 9 8 7 6
24+
11 12 13 14 15
25+
20 19 18 17 16
26+
Sample Input 2:
27+
2
28+
Sample Output 2:
29+
1 2 3 4 5
30+
10 9 8 7 6
31+
EXPLANATION:
32+
In the first example, we'll print the first 4 lines of the given pattern.
33+
In the second example, we'll print the first 2 lines of the given pattern.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
int main() {
5+
int n;
6+
cin>>n;
7+
int sumi = 0, sumj = 10;
8+
for(int i=1; i<=n; i++){
9+
if(i%2!=0){
10+
for(int j=sumi+1; j<=sumi+5; j++){
11+
cout<<j<<" ";
12+
}
13+
cout<<endl;
14+
sumi = sumi + 10;
15+
}else{
16+
for(int j=sumj; j>=sumj-4; j--){
17+
cout<<j<<" ";
18+
}
19+
cout<<endl;
20+
sumj = sumj + 10;
21+
}
22+
}
23+
return 0;
24+
}

0 commit comments

Comments
 (0)