In: Computer Science
// 1.
System.out.println("1.");
int max = 5;
for (int n = 1; n <= max; n++) {
System.out.println(n);
}
System.out.println();
// 2.
System.out.println("2.");
int total = 25;
for (int number = 1; number <= (total / 2); number++) {
total = total - number;
System.out.println(total + " " + number);
}
System.out.println();
// 3.
System.out.println("3.");
for (int i = 1; i <= 2; i++) {
for (int j = 1; j <= 3; j++) {
for (int k = 1; k <= 4; k++) {
System.out.print("*");
}
System.out.print("!");
}
System.out.println();
}
System.out.println();
// 4.
System.out.println("4.");
int number = 4;
for (int count = 1; count <= number; count++) {
System.out.println(number);
number = number / 2;
}
Question:
System.out.println("1.");
int max = 5;
for (int n = 1; n <= max; n++) {
System.out.println(n);
}
System.out.println();
// 2.
System.out.println("2.");
int total = 25;
for (int number = 1; number <= (total / 2); number++) {
total = total - number;
System.out.println(total + " " + number);
}
System.out.println();
// 3.
System.out.println("3.");
for (int i = 1; i <= 2; i++) {
for (int j = 1; j <= 3; j++) {
for (int k = 1; k <= 4; k++) {
System.out.print("*");
}
System.out.print("!");
}
System.out.println();
}
System.out.println();
// 4.
System.out.println("4.");
int number = 4;
for (int count = 1; count <= number; count++) {
System.out.println(number);
number = number / 2;
}
The output of the following code is:
1.
1
2
3
4
5
2.
24 1
22 2
19 3
15 4
10 5
3.
****!****!****!
****!****!****!
4.
4
2
Let's trace the code:
In the first line, We are printing the string "1."
Then we are using a for loop with loop variable n from n=1 to
max(which is 5) inclusive
Then printing the value of n each in a new line
In the first line, we are printing the string "2."
Then we are using a for loop with loop variable number from
number=1 to total/2 which is 25/2 ==> 12
Then Inside the loop, we are updating the value of total by
total-number and printing the value of total as well as number
seperated by a space.
for number=1, It prints 24
for number=2, It prints 22
for number=3, It prints 19
for number=4, It prints 15
for number=5, It prints 10
In the first line, we are printing the string "3."
Then we are using three loops:
outer most loop iterating from i=1 to 2
Next inner loop iterating from j=1 to 3
inner most loop iterating from k=1 to 4
Then we are printing * for the inner loop.
After exiting the inner most loop, we are printing !
After exiting the second loop, we are printing new line.
Basically, there will be 2 lines which consists of ! 3x12->*
with 3!
In the first line, we are printing the string "4."
Then we are using a for loop with loop variable count from count=1
to number(which is 4)
Then Inside the loop, we are updating the value of number as
number/2 after printing the value of number
for count=1, It prints 4 and then number is updated as number/2
which is 2
for count=2, It prints 2 and then number is updated as number/2
which is 1
for count=3 It fails and the loop terminates.
Please check the compiled program and its output for your reference:
Output:
(Feel free to drop me a comment, If you need any help)
Hope this Helps!!!
Please upvote as well, If you got the answer?
If not please comment, I will Help you with that...