Use loop structures below to print the even numbers 2 to 100.(2 4 6 8 10, etc Ea
ID: 3750792 • Letter: U
Question
Use loop structures below to print the even numbers 2 to 100.(2 4 6 8 10, etc Each program will have the same output. Write programs that print the same output as that program, but do it in the following ways A. Using a for loop that increments the loop control variable by 2 each iteration B. Using a for loop whose loop control variable goes from 0 to 50 C. Using a for loop whose loop control variable goes from 100 down too. D. Using an infinite for loop with no conditional expression and exiting the loop with a break statement. E. Using a while loop. F. Using a do-while loop.Explanation / Answer
(A)
package looping;
public class Looping {
public static void main(String[] args) {
int i;
for(i=2;i<=100;i+=2)
{
System.out.println(i);
}
}
}
In this particular answer our looping variable is i which is initialized to 2 and it is incremented by 2 in each iteration.
i+=2 means i=i+2, we are increamenting i by 2.
(B)
package looping;
public class Looping {
public static void main(String[] args) {
int i;
int value=2;
for(i=0;i<50;i++)
{
System.out.println(value);
value+=2;
}
}
}
here, i is again a looping variable. so according to question we are moving i from 0 to 50 we are starting from 0 thats why i have written i<50 not i<=50...
i have taken another variable value, value will store the value 2 and will increment bu 2 with each iteration. value is printed on the screen.
(C)
package looping;
public class Looping {
public static void main(String[] args) {
int i;
int value=2;
for(i=100;i>0;i-=2)
{
System.out.println(value);
value+=2;
}
}
}
here, again we are using value variable to print and increament by 2 with each iteration.
now iterative variable is again i. so we are starting by 100 and coming down to 0 but we are decrementing by 2 because we need only 50 iterations not 100.
i-=2 means, i=i-2
(D)
package looping;
public class Looping {
public static void main(String[] args) {
int value=2;
for(;;)
{
System.out.println(value);
if(value==100)
{
break;
}
value+=2;
}
}
}
here, we have created infinite for loop, for(;;) like this.
we are printing the value which is assigned to 2 initially and incrementing by 2 with each iteration. now we need to stop at 100 because it will continue as it is an infinite loop. so when the value is 100 we are breaking out of the loop using break statement.
(E)
package looping;
public class Looping {
public static void main(String[] args) {
int i=2;
while(i<=100)
{
System.out.println(i);
i=i+2;
}
}
}
The same task is performed with while loop. taking i as 2. printing it and incrementing by 2.
(F)
package looping;
public class Looping {
public static void main(String[] args) {
int i=2;
do
{
System.out.println(i);
i=i+2;
}
while(i<=100);
}
}
The same task is performed with do while loop.
do while loop is similar to while loop, just the check condition is after the body of the loop. so the number of iterations will be 1 less than while and for but there would be no change in output.
output of all questions:
run:
2
4
6
8
10
12
14
16
18
20
22
24
26
28
30
32
34
36
38
40
42
44
46
48
50
52
54
56
58
60
62
64
66
68
70
72
74
76
78
80
82
84
86
88
90
92
94
96
98
100
BUILD SUCCESSFUL (total time: 0 seconds)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.