-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay_1_For_loop.java
More file actions
104 lines (102 loc) · 3.07 KB
/
Day_1_For_loop.java
File metadata and controls
104 lines (102 loc) · 3.07 KB
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
// Welcome to Day 1
// "Today, I tackled a problem involving a for loop.”
/* Problem 1 : print pattern 1
12
123
1234
12345
*/
/* Problem 2 : print pattern 1
121
12321
1234321
123454321
*/
/* Problem 3 : print pattern * * * * *
* *
* *
* *
* * * * *
*/
/* Problem 4 : print pattern *
***
*****
********
***********
********
******
***
*
*/
/* Problem 5 : print factorial (example = 5! = 5*4*3*2*1 = 120)
*/
import java.util.Scanner;
public class Day_1_For_loop {
public static void main(String[] args) {
// Solving Problem 1
for (int i=1 ; i<=5 ; i++){
for (int j=5 ; j>i ; j--){
System.out.print(" ");
}
for (int k=1 ; k<=i ;k++){
System.out.print(k);
}
System.out.println();
}
// Solving Problem 2
for (int i=1 ; i<=5 ; i++) {
for (int j = 5; j > i; j--) {
System.out.print(" ");
}
for (int k = 1; k <= i; k++) {
System.out.print(k);
}
for (int l=i-1 ;l>=1 ; l--){
System.out.print(l);
}
System.out.println();
}
// Solving Problem 3
for (int i=1 ;i<=5 ; i++ ){
for ( int j=1 ;j<=5 ;j++ ){
if ( i==1 || i==5 || j==1 || j==5){
System.out.print("*");
}
else {
System.out.print(" ");
}
}
System.out.println();
}
// Solving Problem 4
for (int i=1 ; i<= 5 ;i++){
for (int j =5 ; j > i ; j--){
System.out.print(" ");
}
for (int k=1 ;k <=2*i-1 ; k++){
System.out.print("*");
}
System.out.println();
}
for (int i = 5-1 ; i>= 1 ; i--){
for (int j = 5 ;j>i ; j--){
System.out.print(" ");
}
for (int k=1 ; k<=2*i-1 ;k++){
System.out.print("*");
}
System.out.println();
}
// Solving Problem 5
Scanner sc =new Scanner(System.in);
int n;
int f=1;
System.out.println("Enter any number for finding factorial = ");
n = sc.nextInt();
for (int i=n ; i>=1 ; i--){
f = f*i;
}
System.out.println("Factorial of "+n+" is = "+ f);
f=1;
}
}