-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathloops.java
More file actions
121 lines (96 loc) · 2.73 KB
/
Copy pathloops.java
File metadata and controls
121 lines (96 loc) · 2.73 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import java.util.Random;
import java.util.Scanner;
// Nested For Loop
// Teacher 1 : Asked you wirte the word pen 5 times
// Teacher 2 : Asked you wirte the word pen 5 times
class NestForLoop1 {
public static void main(String[] args) {
for(int Teacher=1; Teacher<=2; Teacher=Teacher+1)
{
for(int count=1; count<=5; count=count+1)
{
System.out.println("Pen");
}
System.out.println("-----");
}
}
}
// * * *
// * * *
// * * *
class NestForLoop2{
public static void main(String[] args) {
String s = "* ";
for(int j = 1; j <= 3; j++)
{
for(int i = 1; i <= 3; i++)
{
System.out.print("* ");
}
System.out.println();
}
}
}
// *
// * *
// * * *
class NestForLoop3{
public static void main(String[] args) {
for(int j=1; j<=3; j=j+1)
{
for(int i=1; i<=j; i=i+1)
{
System.out.print("* ");
}
System.out.println();
}
}
}
// -- While Loop --
// Print 1 to 10 in while loop
class Whileloop1{
public static void main(String[] args) {
int j = 1;
while(j <= 10){
System.out.println(j);
j=j+1;
}
}
}
// Generate a random number until the number generated random number is 5
class Whileloop2{
public static void main(String[] args) {
Random rand = new Random();
int num = 0; // Frist number
while (num != 5)
{
num = rand.nextInt(11); // last number
System.out.println(num);
}
}
}
// --- Do While loop ---
class DoWhileLoop{
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int count = 0;
do{
System.out.println("Enter the Number > 10 : ");
count =scan.nextInt();
}
while (count < 10); // Correctly use lowercase 'while'
System.out.println("You entered a valid number: " + count);
scan.close(); // Close the scanner to avoid resource leaks
}
}
// Enhanced for loop
class Enhancedforloop{
public static void main(String[] args) {
int num[] = {12,34,56,78,54,21};
System.out.println(num);
for(int Var : num)
{
System.out.println(Var);
}
}
}