72.5k views
1 vote
Suppose that the first number of a sequence is x, where x is an integer. Define ; ‍ if is even; ‍ ‍ ‍ if is odd. Then there exists an integer k such that ‍ . Write a program that prompts the user to input the value of x. The program outputs the integer k such that ‍ and the numbers . (For example, if ‍ , then ‍ , and the numbers , respectively, are 75, 226, 113, 340, 170, 85, 256, 128, 64, 32, 16, 8, 4, 2, 1.) Test your program for the following values of x: 75, 111, 678, 732, 873, 2048, and 65535.

User Hoang Dao
by
5.6k points

1 Answer

4 votes

Answer:

The program is written in c++ , go to the explanation part for it, the output can be found in the attached files.

Step-by-step explanation:

C++ Code:

#include <iostream>

using namespace std;

int main() {

int x;

cout<<"Enter a number: ";

cin>>x;

int largest = x;

int position = 1, count = 0;

while(x != 1)

{

count++;

cout<<x<<" ";

if(x > largest)

{

largest = x;

position = count;

}

if(x%2 == 0)

x = x/2;

else

x = 3*x + 1;

}

cout<<x<<endl;

cout<<"The largest number of the sequence is "<<largest<<endl;

cout<<"The position of the largest number is "<<position<<endl;

return 0;

}

Suppose that the first number of a sequence is x, where x is an integer. Define ; ‍ if-example-1
Suppose that the first number of a sequence is x, where x is an integer. Define ; ‍ if-example-2
User Chaos Monkey
by
5.3k points