-
Notifications
You must be signed in to change notification settings - Fork 10
/
11_hollow_rhombus_pattern.cpp
109 lines (85 loc) · 2.78 KB
/
11_hollow_rhombus_pattern.cpp
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/*
Hollow Rhombus Pattern
Given number of rows N, you have to print a Hollow Rhombus. See the output for corresponding given input.
Input Format: Single integer N.
Constraints: N <= 20
Output Format: Print pattern.
Sample Input: 5
Sample Output: *****
* *
* *
* *
*****
*/
#include<iostream>
using namespace std;
int main() {
int total_row;
cin >> total_row;
int nos = total_row-1; // number of space in first row
int nop = total_row; // number of pattern in first row
for(int row=1; row<=total_row; row++){
int cos; // counter of space
int cop; // counter of pattern
// print spaces
for(cos=1; cos<=nos; cos++){
cout << " ";
}
// print pattern
if(row==1 || row==total_row){
for(cop=1; cop<=nop; cop++){
cout << "*";
}
}else{
cout << "*";
}
// print spaces
for(cos=1; cos<=total_row-2; cos++){
cout << " ";
}
// print pattern
if(row>1 && row<total_row)
cout << "*";
// for next iterations
nos--;
cout<<endl;
}
return 0;
}
/*
Given pattern is seen as fist spaces and then stars for first and last row.And as first spaces then star then spaces and then star for in between rows.Thus, do the work for spaces and then for stars and then for spaces and stars.And do preparation accordingly for next iterations.
Code:
import java.util.Scanner;
public class HollowRohmbusPattern {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
int n = scn.nextInt();
int nst = n; //initializing number of stars
int nsp = n - 1; // initializing number of spaces
for (int i = 1; i <= n; i++) {
// work for spaces
for (int csp = 1; csp <= nsp; csp++) {
System.out.print(" ");
}
// work for stars
for (int cst = 1; cst <= nst; cst++) {
System.out.print("*");
}
if (i > 1 && i < n) {
for (int csp = 1; csp <= n - 2; csp++) {
System.out.print(" ");
}
System.out.print("*");
}
// preparation for next iteration
if (i >= 1 && i < n - 1) {
nst = 1; // taking number of stars as 1 for in between rows
} else {
nst = n; //taking number of stars as n for last row
}
nsp = nsp - 1;
System.out.println();
}
}
}
*/