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

(1) output :

4

1

0

1

4

Explanation : intitially x is -2 then we check if x is less than or equal to 2 since -2 is less than 2 , while loop starts and x*x is printed which is (-2)*(-2)=4 . Hence 4 is printed then we increment x by 1 so x becomes -2+1=-1

Then again while loop starts since -1 is less than 2, x*x= (-1*-1)=1.Hence 1 is printed then we increment x by 1 so x becomes -1+1=0

Then again while loop starts since 0 is less than 2, x*x= (0*0)=0.Hence 0 is printed then we increment x by 1 so x becomes 0+1=1

Then again while loop starts since 1 is less than 2, x*x= (1*1)=1.Hence 1 is printed then we increment x by 1 so x becomes 1+1=2

Then again while loop starts since 2 is equal to 2, x*x= (2*2)=4.Hence 4 is printed then we increment x by 1 so x becomes 2+1=3

Now since 3 is not less than or equal to 2 , while loop exits/stops executing.

(2) output :

2

8

Explanation : intitially x is 1 and we declare value of y equals to value of x , so y=1.Then we check if x is less than or equal to 3. since x=1 is less than 3 , while loop starts and prints (x+y)=1+1=2

Then we calculate y=x*5=1*5=5

And x=x+2=1+2=3

Now x=3,y=5

since x=3 is equal to 3 which is while loop condition , so while loop keeps running and x+y=3+5=8 is printed.

Then we calculate y=x*5=3*5=15

And x=x+2=3+2=5

Now x=5,y=15

since x=5 is not less than or equal to 3 which is while loop condition , so while loop exits i.e. we come out of while loop.

(3) output :

0,1

1,2

2,5

Explanation : intitially x is 0 and y is -2 .since x=0 is less than 2 which is while loop condition , while loop starts.

y is calculated as y=x*x+1=0*0+1=1

x,y is printed so 0,1 is printed since x=0 and y=1

x is again calculated as x=x+1=0+1=1 . so now x becomes x=1

since x=1 is less than 2 , while loop starts again ,

y is calculated as y=x*x+1=1*1+1=2

x,y is printed so 1,2 is printed since x=1 and y=2

x is again calculated as x=x+1=1+1=2 . so now x becomes x=2

since x=2 is equal to 2 , while loop starts again ,

y is calculated as y=x*x+1=2*2+1=5

x,y is printed so 2,5 is printed since x=2 and y=5

x is again calculated as x=x+1=2+1=3 . so now x becomes x=3

since x=3 is not less than or equal to 2 which is while loop condition , so while loop exits i.e. we come out of while loop.

(4) output :

-1

  -3

-12

-21

-30

Explanation : intitially x is 2 and y is x+1=2+1=3. since x=2 is less than 14 which is while loop condition , while loop starts.

(x-y) is printed which is (2-3)= -1

y is again recalculated as y=x*4=2*4=8

x is again recalculated as x=x+3=2+3=5

since x=5 is less than 14 , while loop starts again ,

(x-y) is printed which is (5-8)= -3

y is again recalculated as y=x*4=5*4=20

x is again recalculated as x=x+3=5+3=8

since x=8 is less than 14 , while loop starts again ,

(x-y) is printed which is (8-20)= -12

y is again recalculated as y=x*4=8*4=32

x is again recalculated as x=x+3=8+3=11

since x=11 is less than 14 , while loop starts again ,

(x-y) is printed which is (11-32)= -21

y is again recalculated as y=x*4=11*4=44

x is again recalculated as x=x+3=11+3=14

since x=14 is equal to 14 , while loop starts again ,

(x-y) is printed which is (14-44)= -30

y is again recalculated as y=x*4=14*4=56

x is again recalculated as x=x+3=14+3=17

since x=17 is not less than or equal to 14 which is while loop condition , so while loop exits i.e. we come out of while loop.

(5) output :

Here will be printed 45 times

Explanation : Here inner while loop will run 9 times since y=1 and inner while loop will run until y<10 so it will run 9 times.And outer while loop wil run 5 times since x=1 and outer while loop will run until x is less than or equal to 5.

so when first outer while loop starts , inner while loop will run 9 times

similary when 5 outer while loop runs. inner while loop will run 5*9=45 times , printing Here 45 times.

(6) output:

10

Explanation : intially sum is 0. for loop runs 10 times since i=1 and it checks if i<=10 .sum=sum+1 will be calculated 10 times. each time we update sum value and increment i

sum=0

when i =1 , sum=sum+1=0+1=1

when i =2 , sum=sum+1=1+1=2

when i =3 , sum=sum+1=2+1=3

when i =4 , sum=sum+1=3+1=4

when i =5 , sum=sum+1=4+1=5

when i =6 , sum=sum+1=5+1=6

when i =7 , sum=sum+1=6+1=7

when i =8 , sum=sum+1=7+1=8

when i =9 , sum=sum+1=8+1=9

when i =10 , sum=sum+1=9+1=10

Execution then stops since i=10 will be incremented to 11 and for loop condition is not met which is i<=10

(7) output :

0

4

16

36

64

100

Explanation :

when i =0 , i*i=0*0=0 is printed , i+=2 i.e i is incremented by 2 so i=0+2=2

when i =2 , i*i=2*2=4 is printed , i+=2 i.e i is incremented by 2 so i=2+2=4

when i =4 , i*i=4*4=16 is printed , i+=2 i.e i is incremented by 2 so i=4+2=6

when i =6 , i*i=6*6=36 is printed , i+=2 i.e i is incremented by 2 so i=6+2=8

when i =8 , i*i=8*8=64 is printed , i+=2 i.e i is incremented by 2 so i=8+2=10

when i =10 , i*i=10*10=100 is printed , i+=2 i.e i is incremented by 2 so i=10+2=12

Execution stops since i>10 now and for loop condition is not met which is i<=10

(8)

output :

96

107

118

129

1310

1411

Explanation :

when i =6 , i+3=6+3=9 is printed followed by i which is 6 so 96 will be printed on same line(Note- print command is used not println to print 9 so after printing 9 we will not go to next line) and  i is incremented by 1 so i=7

when i =7 , i+3=7+3=10 is printed followed by i which is 7 so 107 will be printed on same line(Note- print command is used not println to print 10 so after printing 10 we will not go to next line) and  i is incremented by 1 so i=8

when i =8 , i+3=8+3=11 is printed followed by i which is 8 so 118 will be printed on same line(Note- print command is used not println to print 11 so after printing 11 we will not go to next line) and  i is incremented by 1 so i=9

when i =9 , i+3=9+3=12 is printed followed by i which is 9 so 129 will be printed on same line(Note- print command is used not println to print 12 so after printing 12 we will not go to next line) and  i is incremented by 1 so i=10

when i =10 , i+3=10+3=13 is printed followed by i which is 10 so 1310 will be printed on same line(Note- print command is used not println to print 13 so after printing 13 we will not go to next line) and  i is incremented by 1 so i=11

when i =11 , i+3=11+3=14 is printed followed by i which is 11 so 1411 will be printed on same line(Note- print command is used not println to print 14 so after printing 14 we will not go to next line) and  i is incremented by 1 so i=12

Execution stops since i>11 now and for loop condition is not met which is i<=11

(9) output:

15141312111098765432

Explanation : intially j=15 and it is decremented by 1 after printing j until j is less than 1

so 15 then 14 then 13 then 12 so on will be printed untill it less than 1 i.e 2

We have used print command(not println) so output is printed on same line.

(10)output :

***

***

***

***

***

Note : here if you dont declare j as int then error will be thrown instead of above output

for( j=1; j <=3; j++) should be for(int j=1; j <=3; j++)

Explanation : Here i represents no of rows and j represents no of colums .

when i = 1 , inner loop will run 3 times printing * followed by space thrice since inner loop condition is j =1 and j<=3.

when i = 2 , inner loop will run 3 times printing * followed by space thrice since inner loop condition is j =1 and j<=3.

when i = 3 , inner loop will run 3 times printing * followed by space thrice since inner loop condition is j =1 and j<=3.

when i = 4 , inner loop will run 3 times printing * followed by space thrice since inner loop condition is j =1 and j<=3.

when i = 5 , inner loop will run 3 times printing * followed by space thrice since inner loop condition is j =1 and j<=3.

so total 5 rows will be printed and in each row there will be stars followed by space combination printed thrice


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; int y = -2; while (x <= 2) { y = x * x + 1;...
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