forked from DPrinceKumar/HacktoberFest2020-1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Diamond-Patterns.cpp
102 lines (73 loc) · 1.44 KB
/
Diamond-Patterns.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
// C++ program to print diamond shape
// with 2n rows
#include <bits/stdc++.h>
using namespace std;
// Prints diamond pattern with 2n rows
void printDiamond(int n)
{
int space = n - 1;
// run loop (parent loop)
// till number of rows
for (int i = 0; i < n; i++)
{
// loop for initially space,
// before star printing
for (int j = 0;j < space; j++)
cout << " ";
// Print i+1 stars
for (int j = 0; j <= i; j++)
cout << "* ";
cout << endl;
space--;
}
space = 0;
for (int i = n; i > 0; i--)
{
for (int j = 0; j < space; j++)
cout << " ";
for (int j = 0;j < i;j++)
cout << "* ";
cout << endl;
space++;
}
}
int main()
{
printDiamond(5);
return 0;
}
..//java
import java.util.*;
class diamond
{
static void printDiamond(int n)
{
int space = n - 1;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < space; j++)
System.out.print(" ");
for (int j = 0; j <= i; j++)
System.out.print("* ");
System.out.print("\n");
space--;
}
space = 0;
for (int i = n; i > 0; i--)
{
// loop for initially space,
// before star printing
for (int j = 0; j < space; j++)
System.out.print(" ");
// Print i stars
for (int j = 0; j < i; j++)
System.out.print("* ");
System.out.print("\n");
space++;
}
}
public static void main(String[] args)
{
printDiamond(5);
}
}