In: Computer Science
assume that the following pseudocode is executed in a
program
module main
declare x = 1
declare sum = 0
declare max = 5
while ( x < max)
Display "x is ", x
set x = x + 1
set sum = s + x
end while
end module
after the execution of this program the final value of x is_____ and the final value of sum is_____
Solution) Dear Student, after execution
FINAL VALUE OF X = 5 and
FINAL VALUE OF SUM=14
we can get the solution by writing a simple c++ code:
#include <iostream>
using namespace std;
int main(){
int x=1, sum=0; //declaration of x and sum.
while(x < 5){
cout<<"now x is:"<<x<<endl; //x will be printing for each iteration
//which is till 1 to 4.
x=x+1;
sum = sum + x;
cout<<x<<endl; //Value of x
cout<<sum<<endl; //Value of sum
}
return 0;
}
Sample output:
now x is:1
2
2
now x is:2
3
5
now x is:3
4
9
now x is:4
5 //Final Value of x
14 //Final value of sum