Question: Make a program in C ++, using a while loop as an iterative statement. Given a positive integer N, the problem 3n + 1 consists

Make a program in C ++, using a while loop as an iterative statement.

Given a positive integer N, the problem 3n + 1 consists of determining whether it is possible to arrive at N = 1 by repeatedly applying the following operations:

If N is even, divide it by 2. If N is odd, multiply it by 3 and add 1 to it.

For example, if N starts at 26 then N takes the values 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1. It is believed that it is always possible to get to 1 no matter what positive integer you start at (Collatz's Conjecture).

Write a program that calculates the number of steps it takes for N to become 1 and the largest value that N takes during the calculation.

Entry An integer N. You can assume that 1 N 1000.

Exit Two integers denoting the number of steps it takes for N to become 1 and the largest value that N takes during computation.

Example:

Entry -one -26 -27

Exit -0 1 -10 40 -111 9232

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!