I am having difficulties with this lab so so if anyone can help, it would be greatly appreciated.
Given a positive integer n, the following rules will always create a sequence that ends with 1, called the hailstone sequence:
If n is even, divide it by two
If n is odd, multiply it by 3 and add 1 (i.e. 3n +1)
Continue until n is 1
Write a program that reads an integer as input and prints the hailstone sequence starting with the integer entered. Format the output so that ten integers, each separated by a tab character (\t), are printed per line. End the last line of output with a new line (endl).
The output format can be achieved as follows:
cout << n << "\t";
#include <iostream>
usingnamespace std;
int main()
{
int n, count = 1; // count = 1 as it should reflect the number of outputs
cin >> n;
cout << n << "\t"; // Output your first number BEFORE starting the loop
while( n > 1 ) // ...
{
if ( n% 2 == 0 )
n /= 2;
else
n = 3 * n + 1; // Remove excessive bracketing; it's hard to read
cout << n << "\t";
++count;
// cout << endl; // NO. Test before forcing a line break
if ( count % 10 == 0 ) // Test for line breaks WITHIN your main loop
{
cout << endl;
}
}
// cout << n << "\t"; // NO. n was already output in the loop.
cout << endl;
// return 0; // Not necessary
}