Lab 7

 

  1. What is the output of the following C++ code?

count = 1;

y = 100;

while (count < 100)

{

            y = y –1;

count++;

            }

            cout<< “ y= “<<y << “ and count = “ <<count << endl;

 

  1. What is the output in each of the following cases?  Also indicate how many times each of the following loops execute.

a)

            x =5;    y = 50;

            do

                        x = x +10;

            while (x < y);

            cout << x << “ “ << y << endl;

b)

            x =5;    y = 80;

            do

                        x = x * 2;

            while (x < y);

            cout << x << “ “ << y << endl;

 

c)

            x =5;    y = 20;

            do

                        x = x + 2;

            while (x >= y);

            cout << x << “ “ << y << endl;

 

d)

            x =5;    y = 35;

            while (x < y)

                        x=x + 10;

            cout << x << “ “ << y << endl;

 

e)

            x =5;    y = 30;

            while (x <= y)

                        x=x + 2;

            cout << x << “ “ << y << endl;

 

f)         

            x =5;    y = 30;

            while (x > y)

                        x=x + 2;

            cout << x << “ “ << y << endl;

 

  1. Write a program that produces the following output

aaaaa

aaaa

aaa

aa

a

.

  1. Write a program that prompts the user to input an integer and then outputs both the individual digits of the number and the sum of the digits.  For example, it should output the individual digits of 3456 as 3 4 5 6, output the individual digits of 8030 as 8 0 3 0, output the individual digits of 2345526 as 2 3 4 5 5 2 6, output the individual digits of 4000 as 4 0 0 0, and output the individual digits of –2345 as 2 3 4 5.

 

  1. Let n = akak-1ak-2…a1a0 be an integer and t = a0 – a1 + a2 - … + (-1)kak .  It is known that n is divisible by 11 if

and only if t is divisible by 11.  For example, suppose that n = 8784204.  Then t = 4-0 +2 – 4 + 8 – 7 + 8 = 11.  Because 11 is divisible by 11, it follows that 8784204 is divisible by 11.If n = 54063297, then

7 – 9 +2 – 3 + 6 – 0 + 4 – 5 = 2.  Because 2 is not divisible by 11, 54063297 ids not divisible by 11.  Write a program that prompts the user to enter a positive integer and then uses this criterion to determine whether the number is divisible by 11.  If the user enters a negative number the program should print out “you have entered a negative number and then prompt the user again for a positive number to check..