-
Notifications
You must be signed in to change notification settings - Fork 5
/
Patterns and Series in Java
90 lines (86 loc) · 2.41 KB
/
Patterns and Series in Java
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
import java.util.Scanner;
class MorePatterns {
public static void main(String[] args) {
// *******************
// For Diamond Pattern
// *******************
int n, i, j, space = 1;
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number of rows for Diamond: ");
n = sc.nextInt();
space = n - 1;
for (j = 1; j<= n; j++)
{
for(i = 1; i<= space; i++)
{
System.out.print(" ");
}
space--;
for(i = 1; i <= 2 * j - 1; i++)
{
System.out.print("*");
}
System.out.println("");
}
space = 1;
for(j = 1; j<= n - 1; j++)
{
for(i = 1; i<= space; i++)
{
System.out.print(" ");
}
space++;
for(i = 1; i<= 2 * (n - j) - 1; i++)
{
System.out.print("*");
}
System.out.println("");
}
//**********************
// For Sandglass Pattern
//**********************
System.out.print("Enter the number of rows for Sandglass: ");
int rows = sc.nextInt();
for (int x= 0; x<= rows-1 ; x++)
{
for (int y=0; y <x; y++)
{
System.out.print(" ");
}
for (int z=x; z<=rows-1; z++)
{
System.out.print("*" + " ");
}
System.out.println("");
}
for (int x= rows-1; x>= 0; x--)
{
for (int y=0; y< x ;y++)
{
System.out.print(" ");
}
for (int z=x; z<=rows-1; z++)
{
System.out.print("*" + " ");
}
System.out.println("");
}
//***************************
// For Number series with sum
//***************************
System.out.print("Enter the number of rows for Number series: ");
int row_no = sc.nextInt();
int c = 1;
for(int a = 0; a < 5; a++)
{
int sum = 0;
for(int b = 0; b < row_no; b++)
{
System.out.print(c + " ");
c++;
sum += c;
}
System.out.print(" = " + sum + "\n");
}
}
}