1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
NeX [460]
2 years ago
9

The length of a hailstone sequence is the number of terms it contains. For example, the hailstone sequence in example 1 (5, 16,

8, 4, 2, 1) has a length of 6 and the hailstone sequence in example 2 (8, 4, 2, 1) has a length of 4. Write the method hailstoneLength(int n), which returns the length of the hailstone sequence that starts with n. /** Returns the length of a hailstone sequence that starts with n, * as described in part (a). * Precondition: n > 0 */ public static int hailstoneLength(int n)
Computers and Technology
1 answer:
11111nata11111 [884]2 years ago
7 0

Answer:

Following are the program to this question:

#include <iostream> //defining header file

using namespace std;

int hailstoneLength(int n) //defining method hailstoneLength

{

int t=1; //defining integer variable assign  

while(n!=1) //define a loop that checks value is not equal to 1

{

if(n%2==0) // check even number condition

{

n=n/2; // divide the value by 2 and store its remainder value in n

t++; //increment value of t by 1.

}

else

{

n=n*3+1; //calculate and hold value in n  

t++; //increment value of t variable by 1  

}

}

return t; //return value

}

int main() //defining main method

{

int n; //defining integer variable

cout<<"Enter any number: "; //print message

cin>> n; //input value

cout<<hailstoneLength(n); //call method and print its value

return 0;

}

Output:

Enter any number: 3

8

Explanation:

Program description can be given as follows:

  • In the given C++ language program an integer method "hailstoneLength", is declared, that accepts an integer variable "n" in its parameter.
  • Inside the method, an integer variable t is declared, that assign a value that is 1, in the next line, a while loop is declared, that uses if block to check even condition if number is even it divide by 2 and increment t variable value by 1.
  • If the number is odd it will multiply the value by 3 and add 1 and increment t by 1 then it will go to if block to check value again. when value of n is not equal to 1 it will return t variable value.
  • In the main method, an integer variable "n" is used that call the method and print its return value.
You might be interested in
Which system provides an easier way for people to communicate with a computer than a graphical user interface (GUI)
Marta_Voda [28]
Answer: natural Language Processing

Explanation:

Natural Language Processing: It is a sub field of linguistics which is concerned with the interactions between Human and computer especially how to program computers to process and to understand large amounts of natural language data.
6 0
2 years ago
ASAP
grin007 [14]
I would recommend A. Quickly on the left and move ahead of it because you should always pass on the left and seeing as it is a large truck, you would want to get ahead of it ASAP.
4 0
3 years ago
Read 2 more answers
Yo I need to know where to find a ps4 for free or a very cheap price please
svetlana [45]
Download offer up from the apple store or play store and search that up there’s many good deals and sometimes comes with games and controls included for cheap!
8 0
2 years ago
Read 2 more answers
What happened to price during the month of September 2018
torisob [31]
The prices went up alot because of taxes
6 0
3 years ago
________ speeds time to insights and enables better data governance by performing data integration and analytic functions inside
Lyrx [107]
<h3><em>↓Answer↓</em></h3>

<u>In-database analytics</u> speeds time to insights and enables better data governance by performing data integration and analytic functions inside the database.

3 0
1 year ago
Other questions:
  • Think about a time when someone made a biased judgment about you or acted unfairly toward you because of your age, skin color, c
    6·1 answer
  • How does the use of modules provide flexibility in a structured programming design?
    14·2 answers
  • Christine wants to send a quick communication to all department managers. She should send a _____. report
    13·2 answers
  • Please help! (I cant figure out)
    13·1 answer
  • In what year was google launched on the web?
    14·1 answer
  • The ________ phase is a technical blueprint for a whole system which captures all aspects of how the system's components will fu
    9·1 answer
  • Saving a file as a new filename can be accomplished through the Save As dialog box.
    13·1 answer
  • 4
    12·2 answers
  • I only put one answer and didn’t specify what it answered
    14·2 answers
  • If you want the input gear to spin in the same direction as the output gear you must use a _______ gear.
    13·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!