Question

In: Computer Science

// 1. System.out.println("1."); int max = 5; for (int n = 1; n <= max; n++)...

// 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;
}

Solutions

Expert Solution

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 part:

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 second part:

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 Third part,

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 fourth loop,

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...


Related Solutions

The following pseudocode finds the maximum element in an array of size n. Int MAX (int...
The following pseudocode finds the maximum element in an array of size n. Int MAX (int A [ ], int n) { M = A[0]; for i = 1 to n – 1     if (A[i] > M)         M = A[i]        //Update the max return M; } Write a recursive version of this program. Let f(n) be the number of key comparisons performed by this algorithm. Write a recurrence equation for f(n). Prove by induction that the solution of...
Find the output for the following statements. System.out.println(3%2+1+(3/2)); char ch = 69; System.out.println(ch); int x =...
Find the output for the following statements. System.out.println(3%2+1+(3/2)); char ch = 69; System.out.println(ch); int x = 30; int y = 4; double z = x/y; System.out.println(z);
int a = 3; int b = -2; if((a>0)&&(b>0)){ if (a>b) { System.out.println("A"); } else {...
int a = 3; int b = -2; if((a>0)&&(b>0)){ if (a>b) { System.out.println("A"); } else { System.out.println("B"); } } else if ((b<0)||(a<0)) { System.out.println("C"); } else { System.out.println("D"); }
Please explain this code line by line void printperm(int *A,int n,int rem,int j) {    if(n==1)...
Please explain this code line by line void printperm(int *A,int n,int rem,int j) {    if(n==1)       {        for(int k=0;k<j;k++)        cout<<A[k]<<" + ";        cout<<rem<<"\n";        return;       }     for(int i=0;i<=rem;i++)    {          if(i<=rem)          A[j]=i;          printperm(A,n-1,rem-i,j+1);    } }
int main() {    footBallPlayerType bigGiants[MAX];    int numberOfPlayers;    int choice;    string name;   ...
int main() {    footBallPlayerType bigGiants[MAX];    int numberOfPlayers;    int choice;    string name;    int playerNum;    int numOfTouchDowns;    int numOfcatches;    int numOfPassingYards;    int numOfReceivingYards;    int numOfRushingYards;    int ret;    int num = 0;    ifstream inFile;    ofstream outFile;    ret = openFile(inFile);    if (ret)        getData(inFile, bigGiants, numberOfPlayers);    else        return 1;    /// replace with the proper call to getData    do    {       ...
int f2 (int n) j = 0; while (j <n) {for (int i = 0; i...
int f2 (int n) j = 0; while (j <n) {for (int i = 0; i <n; ++ i) {cout << "j =" + j; j = j + 5; }}
Consider the following function: int counter( int n) { int s = 0;    for ( int...
Consider the following function: int counter( int n) { int s = 0;    for ( int i = 0; i < n; i++)             for ( int j = i; j > 0; j--)                        s = s + 1;   return s; } Part (a): How many times "s=s+1;" will be executed? Determine a precise count.  Show all your work. Part (b): What is the time complexity of the "counter" function in terms of Big-O notation? Justify and show all your work.
#include <stdio.h> #define MAX 8 //Byte = 8 bits void func_and(int a[], int b[], int result[])...
#include <stdio.h> #define MAX 8 //Byte = 8 bits void func_and(int a[], int b[], int result[]) { for(int i=0; i < MAX; i = i + 1){ result[i] = a[i] & b[i]; } } void func_or(int a[], int b[], int result[]) { for(int i=0; i < MAX; i = i + 1){ result[i] = a[i] || b[i]; } } void func_not(int a[], int result[]) { for (int i = 0; i < MAX; i = i + 1) { result[i]...
What is time Complexity each of the following function? 1- void function(int n) {             for (int...
What is time Complexity each of the following function? 1- void function(int n) {             for (int i=n/2; i<=n; i++)                           for (int j=1; j<=n/2; j++)                                     for (int k=1; k<=n; k = k * 2)                                                 print ”Hello”; } 2- void function(int n) {             for (int i=n/2; i<=n; i++)                           for (int j=1; j<=n; j = 2 * j)                                     for (int k=1; k<=n; k = k * 2)                                                 print ”Hello”; } 3- void function(int n) {             for (int i=1; i<=n; i++)                           for (int j=1;...
QUESTION 5 What is printed? int[] aArray = {1, 2, 3, 4}; int[] bArray = {5,...
QUESTION 5 What is printed? int[] aArray = {1, 2, 3, 4}; int[] bArray = {5, 6, 7, 8}; bArray = aArray; System.out.print(bArray[2]); 1 points    QUESTION 6 What is printed? public static void main(String[] args) { int[] aArray = { 1, 2, 3, 4 }; System.out.print(aArray[2]); int i = aMeth(aArray); System.out.print(i + "" + aArray[2]); } public static int aMeth(int[] iArray) { for (int i = 0; i < iArray.length; i++) { iArray[i]++; } int[] bArray = { 5,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT