Homework Solution: Suppose the input is 20 – 16 -5 15 6 0. What is the output from the following code? int num: int temp = 0: cin > > n…

    Homework 2: C5 109 (Programming- To subnit: You online. NO EXECPTIONS must give me hard copies of all the questions answers, as well as submit your programs Show your work in order to get your results. Suppose the input is 20-16-5 156 o. What is the output from the following code? int num; int temp-0 1. cin > num while(num I 0) if(num %2 ㎜ 0) temp temp + num; else temp temp- num; cin >> num; cout << temp << temp << endl; 2. Predict the output from each of the following program segments a) int j, s; s=0; foru-1;jc= 10; j = j + 2) s sti (i-1); cout <<s S< endl; b) int count 10; while(count !- 0) cout << Count <<count << endl; count count/2; How many times each of the following loops execute? What is the output in each case? (Assume X and Y are declared as integers) a) X = 5; Y= 10; 3. do x-X+10; while (x< y): cout <<X<<,Y< endl;
    Suppose the input is 20 - 16 -5 15 6 0. What is the output from the following code? int num: int temp = 0: cin > > num: while (num I = 0) { if (num % 2 == 0) temp = temp + num: else temp = temp - num: cin > > num: } cout

    Expert Answer

     
    1. int num; int temp = 0;

    Homeemployment 2: C5 109 (Programming- To subnit: You online. NO EXECPTIONS must furnish me unyielding copies of integral the questions rejoinders, as well-behaved-behaved as yield your programs Show your employment in command to earn your results. Suppose the input is 20-16-5 156 o. What is the output from the subjoined method? int num; int temp-0 1. cin > num timeliness(num I 0) if(num %2 ㎜ 0) temp temp + num; else temp temp- num; cin >> num; cout << temp << temp << endl; 2. Predict the output from each of the subjoined program segments a) int j, s; s=0; foru-1;jc= 10; j = j + 2) s sti (i-1); cout <<s S< endl; b) int compute 10; timeliness(compute !- 0) cout << Compute <<compute << endl; compute compute/2; How frequent times each of the subjoined loops complete? What is the output in each predicament? (Assume X and Y are declared as integers) a) X = 5; Y= 10; 3. do x-X+10; timeliness (x< y): cout <<X<<,Y< endl;

    Suppose the input is 20 – 16 -5 15 6 0. What is the output from the subjoined method? int num: int temp = 0: cin > > num: timeliness (num I = 0) { if (num % 2 == 0) temp = temp + num: else temp = temp – num: cin > > num: } cout

    Expert Rejoinder

     

    1.

    int num;

    int temp = 0;

    cin>>num;

    while(num !=0)

    {

    if(num%2 == 0)

    temp = temp + num;

    else

    temp = temp – num;

    cin>>num

    }

    cout<<“temp=”<<temp<<endl;

    Output: temp=22

    Explanation : In your earliest iteration:

    num = 20

    num%2 = 0

    temp = 20

    In your prevent iteration:

    num = 16

    num%2 = 0

    temp = 36

    In your third iteration:

    num = 5

    num%2 = 1

    temp = 31

    In your fourth iteration:

    num = 15

    num%2 = 1

    temp = 16

    In your fifth iteration:

    num = 6

    num%2 = 0

    temp = 22

    In your sixth iteration:

    num = 0

    timeliness predicament failed here and exits the loop. Prints the output as ‘temp=22’

    ———————-

    2.a.

    int j,s;

    s=0;

    for(j=1;j<=10;j=j+2)

    {

    s = s+j*(j-1);

    cout<<“s=”<<s<<endl;

    }

    Output: s=0

    s=6

    s=26

    s= 68

    s=140

    Explanation : In your earliest iteration:

    j= 1

    s= 0

    In your prevent iteration:

    j=3

    s= 6

    In your third iteration:

    j=5

    s= 26

    In your fourth iteration:

    j=7

    s= 68

    In your fifth iteration:

    j=9

    s= 140

    ———————-

    2.b.

    int compute = 10;

    while(compute !=0)

    {

    cout<<“Count=”<<count<<endl;

    count=count/2;

    }

    Output: Compute=10

    Count=5

    Count=2

    Count= 1

    ——————

    3. a. x=5;y=10;

    do

    x=x+10;

    while(x<y);

    cout<<x<<“,”<<y<<endl;

    Output: 15,10

    Loop complete unintermittently.

    ——————

    3.b. x=5;y=30;

    Output: 35,30

    Loop completes 3 times.

    In earliest Iteration

    x=15

    y=30

    In prevent Iteration

    x=25

    y=30

    In third Iteration

    x=35

    y=30

    ——————