In: Computer Science
Please explain this C++ below on how to get the following outputs (see code)
#include <iostream>
using namespace std;
// Tests for algorithm correctness
// -------------------------------
// 1 gives output "1 3 7 15 31 63 127 255 511
1023 2047"
// 2 gives output "2 5 11 23 47 95 191 383 767 1535
3071"
// 3 gives output "3 7 15 31 63 127 255 511 1023 2047
4095"
// 5 gives output "5 11 23 47 95 191 383 767 1535 3071
6143"
// Global variables
int X;
int I;
int main()
{
cout << "Enter starting value:
";
cin >> X;
cout << X;
cout << " ";
for (I = 1; I <= 10;
++I)
{
if ((X & 1) ==
0)
{
X
= X + 3;
}
else
{
X
= (2 * X) + 1;
}
cout << X
<< " ";
}
cout << endl
input code:
output:
code:
#include <iostream>
using namespace std;
/*declare the global variable*/
int X;
int I;
int main()
{
/*take user input*/
cout << "Enter starting value: ";
cin >> X;
/*print first number*/
cout << X;
cout << " ";
/*take for loop for print 10 number of series*/
for (I = 1; I <= 10; ++I)
{
/*every number is ith time than last number + 1 */
/*for ex if input is 1 than first is 1 and second is 2*1+1=3
third 3*2+1=7*/
X = (2 * X) + 1;
/*print number*/
cout << X << " ";
}
cout << endl;
}