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"); }
This code assigns the maximum of the values 3 and 5 to the int variable max...
This code assigns the maximum of the values 3 and 5 to the int variable max and outputs the result int max; // your code goes here This code prompts the user for a single character and prints "true" if the character is a letter and "false" if it is not a letter // your code goes here
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    {       ...
Find the runtime of this function, where n is an integer. int function(int n) { int...
Find the runtime of this function, where n is an integer. int function(int n) { int a = 0; for (int i = n; i > 0; i--) { for (int j = 0; j < i; j++) { a = a + i + j; } } return a; } Find the runtime of this function, where m and n are two integers. int f(int m, int n) { if (m==1 || n==1) { return 1; } return f(m,...
Analyze the following codes by computing their running time, T(n). 1) int function ( int n)...
Analyze the following codes by computing their running time, T(n). 1) int function ( int n) { int sum; for (int i = 0; i < n; i++) ​ sum = sum + (i * n); return sum; } 2) int function(int n) { ​int sum; ​ ​for (int i = 0; i < n; i++) ​​if (n < 1000) ​​​sum++; ​​else ​​​sum += function(n); } 3) int function(n) { ​int s = 0; ​for (int i = 1; i...
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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT