-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path04. Loops.java
More file actions
116 lines (95 loc) · 2.63 KB
/
04. Loops.java
File metadata and controls
116 lines (95 loc) · 2.63 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
/*
A loop is a sequence of instruction s that is continually repeated until a certain condition is reached.
1. for Loop
2. while Loop
3. do while Loop
for(initialisation; condition; updation) {
//do something
}
while(condition) {
//do something
}
do {
//do something
} while(condition);
In the case of do-while Loop condition is checked later whether the condition is true or false. If false atleast one operation will be done, because usually in do-while loop condition is checked later. But in other loops (eg. for Loop, while Loop) condition is always checked first then the operation is done.
*/
/*-----------<for Loop>-----------*/
import java.util.*;
public class main {
public static void main(String[] args) {
//print hello world 100 times
for(int i=0; i<100; i++) {
System.out.println("Hello World.");
}
}
}
import java.util.*;
public class main {
public static void main(String[] args) {
//infinite loop
for(int i=0; i<100; i++) {
System.out.println("Hello World.");
}
}
}
import java.util.*;
public class main {
public static void main(String[] args) {
//print from 0 to 10
for(int i=0; i<11; i++) {
System.out.println(i);
}
}
}
/*-----------<while Loop>-----------*/
import java.util.*;
public class main {
public static void main(String[] args) {
//print from 0 to 10
int i = 0;
while(i < 11) {
System.out.println(i);
i++;
}
}
}
/*-----------<do-while Loop>-----------*/
import java.util.*;
public class main {
public static void main(String[] args) {
//print from 0 to 10
int i = 0;
do {
System.out.println(i);
i++;
} while(i < 11);
}
}
//Qs. Print the Sum of First n Natural Numbers. [n = 4]
import java.util.*;
public class main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the value of n: ");
int n = sc.nextInt();
int sum = 0;
for(int i=0; i<=n; i++) {
sum = sum + i;
}
System.out.print("Answer is: "+sum);
}
}
//Qs. Print the table of a number input by the user. [n = 2]
import java.util.*;
public class main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the value of n: ");
int n = sc.nextInt();
for (int i=1; i<=10; i++) {
int mul = n*i;
System.out.println(n+"x"+i+" = "+mul);
}
}
}