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;

    Homecomposition 2: C5 109 (Programming- To subnit: You online. NO EXECPTIONS must afford me severe copies of every the questions responses, as well-mannered-mannered as acquiesce your programs Show your composition in command to secure your results. Suppose the input is 20-16-5 156 o. What is the output from the aftercited legislation? int num; int temp-0 1. cin > num time(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 aftercited 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 estimate 10; time(estimate !- 0) cout << Estimate <<estimate << endl; estimate estimate/2; How manifold times each of the aftercited loops consummate? What is the output in each plight? (Assume X and Y are nominal as integers) a) X = 5; Y= 10; 3. do x-X+10; time (x< y): cout <<X<<,Y< endl;

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

    Expert Response

     

    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 foremost 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

    time stipulation 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 foremost 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 estimate = 10;

    while(estimate !=0)

    {

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

    count=count/2;

    }

    Output: Estimate=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 consummate uniformly.

    ——————

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

    Output: 35,30

    Loop consummates 3 times.

    In foremost Iteration

    x=15

    y=30

    In prevent Iteration

    x=25

    y=30

    In third Iteration

    x=35

    y=30

    ——————