Question

In: Computer Science

Without using a computer determine the output for each of the loops. 1. int x =...

Without using a computer determine the output for each of the loops.

1.
int x = -2;
while (x <= 2) {
System.out.println(x * x);
x = x + 1;
}


2.
int x = 1;
int y = x;
while (x <= 3) {
System.out.println(x + y);
y = x * 5;
x = x + 2;
}

3.
int x = 0;
int y = -2;
while (x <= 2) {
y = x * x + 1;
System.out.println(x + “, ” + y);
x = x + 1;
}


4.
int x = 2;
int y = x + 1;
while (x <= 14) {
System.out.println(x – y);
y = x * 4;
x = x + 3;
}

5. How many times will “Here” be printed?
int x = 1;
while (x <= 5) {
int y = 1;
while (y < 10){
System.out.println(“Here”);
y++;
}
x++;
}
6.
int sum = 0;
for (int i = 1; i <= 10; i++)
sum = sum + 1;
System.out.println(sum);





7.
for (int i = 0; i <=10; i += 2)
System.out.println(i * i);





8.
for (int i = 6; i <= 11; i++) {
System.out.print(i + 3);
System.out.println(i);
}





9.
for (int j = 15; j > 1; j--)
System.out.print(j);








10. for (int i= 1; i <= 5; i++){
for( j=1; j <=3; j++)
System.out.print(“*”);
System.out.println(“”);
}

Solutions

Expert Solution

The output for the given loops:

1) int x = -2;

while (x <= 2) {

System.out.println(x * x);

x = x + 1;

}

Solution:

Iteration Number

          x

    x<=2

Println(x*x)

x=x+1

1st

          -2

        -2<=2

         True

-2* -2 = 4

x= -2+1 = -1

2nd

           -1

       -1<=2

          True

-1 * -1 =1

x=-1 + 1 =0

3rd

            0

          0<=2

          True

0*0=0

x=0+1=1

4th

             1

         1<=2

          True

1*1 =1

x=1+1=2

6th

             2

           2<=2

          True

2*2=4

x=2+1=3

7th

              3

            3<=2

            False

Will not execute

Will not execute

Output :

4

1

0

1

4

2) int x = 1;

int y = x;

while (x <= 3) {

System.out.println(x + y);

y = x * 5;

x = x + 2;

}

Solution:

  

Iteration Number

          x

    y

x<=3

Println(x+y)

y=x*5

x=x+2

1st

          1

      1

         

    1<=3

  True

1+1=2

1*5=5

1+2=3

2nd

          3

       5

        

    3<=3

     True

3+5=8

3*5=15

3+2=6

3rd

          6

      15   

       

     6 <=3

      False

Will not execute

Will not execute

Will not execute

Output :

2

8

3) int x = 0;

int y = -2;

while (x <= 2) {

y = x * x + 1;

System.out.println(x + “, ” + y);

x = x + 1;

}

Solution:

Iteration Number

          x

    y

x<=2

y=x*x+1

Print(x+”,”+y)

x=x+1

1st

        0

      -2

0<=2

True

0*0+1=1

0 , 1

0+1=1

2nd

        1

        1

1<=2

True

1*1+1=2

   1 , 2

1+1=2

3rd

        2

        2

2<=2

True

2*2+1=5

   2 , 5

2+1=3

4th

         3

        5

        

3<=2

False

Will not execute

Will not execute

Will not execute

Output:

0 , 1

1 , 2

2 , 5

4) int x = 2;

int y = x + 1;

while (x <= 14) {

System.out.println(x – y);

y = x * 4;

x = x + 3;

}

Solution:

Iteration Number

          x

    y

x<=14

Println(x-y)

y=x*4

x=x+3

1st

          2

       3

2<=14

True

2-3= -1

2*4=8

2+3=5

2nd

          5

      8

5<=14

True

5-8= -3

5*4=20

5+3=8

3rd

          8

       20

   8<=14

True

8-20= -12

8*4=32

8+3=11

4th

          11

       32

11<=14

True

32-11= -21

11*4=44

11+3=14

6th

         14

       44   

         

14<=14

True

14-44= -30

14*4=56

14+3=17

7th

          17

         56  

            

17<=14

False

Will not execute

Will not execute

Will not execute

Output:

-1

-3

-12

-21

-30

5) How many times will “Here” be printed?

int x = 1;

while (x <= 5) {

int y = 1;

while (y < 10){

System.out.println(“Here”);

y++;

}

x++;

}

Solution:

Here the Outer loop gets excuted for 5 times and the inner loop gets executed for 9 times . The Inner while loop is used to print "Here" 9 times . The outer while is used to print these 9 "Here" 5 times.

As soon the condtion for the inner while loop becomes false i.e when y becomes 10 then it comes out of the loop and the outer loop is executed again.

The execution happens like this:

1st Iteration: The outer while loop gets executed 1st time when x=1 and the inner for loop gets executed while y=1,2.....9 and prints "Here "9 times one below the other.

2nd Iteration:The outer while loop gets executed 2nd time when x=2 and the inner for loop gets executed while y=1,2.....9 and prints "Here" 9 times one below the other.

3rd Iteration:The outer while loop gets executed 3rd time when x=3 and the inner for loop gets executed while y=1,2.....9 and prints "Here" 9 times one below the other.

4th Iteration:The outer while loop gets executed 4th time when x=4 and the inner for loop gets executed while y=1,2.....9 and prints "Here" 9 times one below the other.

5th Iteration:The outer while loop gets executed 5th  time when x=5 and the inner for loop gets executed while y=1,2.....9 and prints "Here" 9 times one below the other.

6th Iteration:The condition of the outer loop becomes false i.e i=6 hence it comes out of the loop .

As the outer loop is excuted 5 times and inner loop 9 times so 'Here' gets printed 45 times each 'Here' on next line.

Output:

"Here" is printed 45 times on separate lines.

6) int sum = 0;

for (int i = 1; i <= 10; i++)

sum = sum + 1;

System.out.println(sum);

Solution:

Iteration Number

       sum

    i

i<=10

sum=sum+1

   i++

1st

0

     1

1<=10

True

0+1=1

     2

2nd

1

     2

2<=10

True

1+1=2

      3

3rd

2

      3

3<=10

True

2+1=3

      4

4th

3

      4

4<=10

True

3+1=4

       5

5th

4

       5

5<=10

True

4+1=5

       6

6th

5

       6

6<=10

True

5+1=6

       7

7th

6

       7

7<=10

True

6+1=7

      8

8th

7

      8

8<=10

True

7+1=8

        9

9th

8

        9

9<=10

True

8+1=9

      10

10th

9

      10

10<=10

True

9+1=10

     11

11th

10

     11

11<=10

False

Will not execute

Will not execute

The final Sum value will be printed as output.

Output:

10

7)for (int i = 0; i <=10; i += 2)

System.out.println(i * i);

Solution:

Iteration Number

          i

    i<=10

Println(i*i)

i+=2

1st

         0

0<=10

True

0*0=0

0+2=2

2nd

         2

    2<=10

True

2*2=4

2+2=4

3rd

         4

   4<=10

True

4*4=16

4+2=6

4th

         6

   6<=10

True

6*6=36

6+2=8

6th

         8

8<=10

True

8*8=64

8+2=10

7th

        10

10<=10

True

10*10=100

10+2=12

8th

        12

12<=10

False

Will not execute

Will not execute

Output:

0

4

16

36

64

100

8)for (int i = 6; i <= 11; i++) {

System.out.print(i + 3);

System.out.println(i);

}

Solution:

Iteration Number

          i

    i<=11

Print(i+3)

Println(i)

i++

1st

         6

6<=11

True

6+3=9

         6

        7

2nd

         7

7<=11

True

7+3=10

         7

         8

3rd

         8

8<=11

True

8+3=11

         8

          9

4th

          9

9<=11

True

9+3=12

          9

         10

6th

         10

10<=11

True

10+3=13

         10

         11

7th

         11

11<=11

True

11+3=14

         11

         12

8th

         12

12<=11

False

Will not execute

Will not execute

Will not execute

note:Here print will print the output on same line i.e 9 and println will move to the next line after printing the output on the same line so it becomes 96 and when next time it prints 10 it starts on next line.

Output:

96

107

118

129

1310

1411

9)for (int j = 15; j > 1; j--)

System.out.print(j);

Solution:

Here the for loop gets executed until the condtion becomes false . Now j =15 and after every iteration the value of j is decremented by 1 i.e j =14 then j=13 so this will continue unt


Related Solutions

Without using a computer determine the output for each of the loops. 1.             int x...
Without using a computer determine the output for each of the loops. 1.             int x = -2;             while (x <= 2) {                            System.out.println(x * x);                            x = x + 1;             } 2.               int x = 1;               int y = x;             while (x <= 3) {                            System.out.println(x + y);                              y = x * 5;                              x = x + 2;               } 3.               int x = 0;...
Determine the execution count of the following programs: (number of loops executed?) int i = 10;...
Determine the execution count of the following programs: (number of loops executed?) int i = 10; while(i > 0) { for(int p = 5; p < 14; p++) b{ System.out.println(i + p) p++; } i--; }
class Loops{ public void printNumbers(int low, int high){ // using a for loop, print all numbers...
class Loops{ public void printNumbers(int low, int high){ // using a for loop, print all numbers from low to high for(int i = low; i <= high; i++){ System.out.println(i); } } public int sumOfNumbers(int n){ // using a for loop, calculate and return the sum of first n numbers // i.e n = 5, answer = 5+4+3+2+1 = 15 int sum = 0; for(int i = 1; i <= n; i++){ sum += i; } return sum; } public void...
Can you create a player vs computer Hangman game on MATLAB using nested loops, for loops,...
Can you create a player vs computer Hangman game on MATLAB using nested loops, for loops, if loops, while loops, arrays and conditional execution, as well as creating an image of the game board. The list below must be considered in the code. - Selecting a word from a dictionary (a text file) - Reading a single letter from the user - Building a character array showing the letters matched so far - Keeping count of the number of guessed...
#include <string> using namespace std; //using recursion no loops allowed int main() { double nums[] =...
#include <string> using namespace std; //using recursion no loops allowed int main() { double nums[] = { 13.8, 2.14, 51, 82, 3.14, 1.7, 4.89, 18, 5, 23.6, 17, 48, 5.6 };   //Challenge #2: print the list from given range   printList(nums, 0, 12); //13.8 2.14 51 .... 48 5.6   cout << endl;   //Challenge #3: print the list, but backwards   printReverse(nums, 0, 12); //5.6 48 17 ... 2.14 13.8   cout << endl;                  //Challenge #4: reverse order of items in list   reverse(nums,...
What is the output of the following C program? #include<stdio.h> int fac (int x); void main(...
What is the output of the following C program? #include<stdio.h> int fac (int x); void main( ) {                         for (int i=1; i<=2; i++)                                     printf("%d", fac(i)); } int fac(int x) {                         x = (x>1) ? x + fac(x-1) : 100);                         return x; }
1)What is the output of the following code? struct someType { int a; int b; };...
1)What is the output of the following code? struct someType { int a; int b; }; void f1(someType &s) { s.a = 1; s.b = 2; } someType f2(someType s) { someType t; t = s; s.a = 3; return t; } int main() { someType s1, s2; f1(s1); s2 = f2(s1); cout << s1.a << '-' << s1.b << '-' << s2.a << '-' << s2.b << '-' << endl; return 0; } ------------------------------------------------------------------------------------------------------------------ 2) struct dateType { int...
Without using formulas, explain how you can determine each of the following: c. If you have...
Without using formulas, explain how you can determine each of the following: c. If you have 10 people, how many different committees of 3 people can be formed for the offices of president, vice-president, and secretary? d. Does order matter or not for part c? Explain.
#include using namespace std; int menu(){    int x;    cout<<"1.Add Entry\n";    cout<<"2.Edit Entry\n";   ...
#include using namespace std; int menu(){    int x;    cout<<"1.Add Entry\n";    cout<<"2.Edit Entry\n";    cout<<"3.remove Entry\n";    cout<<"4.print Entry\n";    cout<<"5.Close Console\n";    cout<<"Enter Your Choice:-";    cin>>x;              return x; } int main() {    int x;    int entry[1000];    int entnum = 0;    int num = 0;    int nom =0;     while (1)     {         int choice = menu();         if (choice == 1){            cout<<"A entry " <<...
#include <iostream> using namespace std; int main() {     int hour;     int min;     for (hour = 1;...
#include <iostream> using namespace std; int main() {     int hour;     int min;     for (hour = 1; hour <= 12; hour++)     {         for (min = 0; min <= 59; min++)         {             cout << hour << ":" << min << "AM" << endl;         }     }       return 0; } 1.      Type in the above program as time.cpp. Add a comment to include your name and date. Compile and run. 2.      What is the bug or logic error in the above program? Add the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT