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
Describe how a computer checks whether the image just taken by the camera matches the scanned photograph.
eimsori [14]

Answer:

by it scan properly like you use Camara

when we use camara and it have record time like that it also have that type of scan data so that the computer use

sorry I don't no other thing and I an sorry for the very short answer

5 0
2 years ago
You can place an insertion point by clicking in the field or by pressing ____.
Slav-nsk [51]
You can place an insertion point by clicking in the field or by clicking F2 keyboard shortcut. Insertion point is usually characterized by a blinking vertical line that allows you to insert a next character that you wanted.
4 0
3 years ago
What do you like most about brainly?
Natali5045456 [20]

Getting the answers to my homework :p

4 0
3 years ago
Read 2 more answers
A(n) _____ chart is drawn on the same worksheet as the data.
Harrizon [31]
The answer is An embedded chart
6 0
3 years ago
Six external parts of a computer system, which are output and which are input devices.
Zigmanuir [339]
1. The keyboard is an example of an input device.
2. A mouse is an example of an input device.
3. Display Monitors are examples of output devices as they display information.4. Audio speakers are examples of output devices.5. Headsets with microphones are an example of both an input and output device as they allow sound to be inputted and receive sound.6. Printer/Printer scanner. A printer would be an example of an output device, however, a printer scanner combination would be both input and output.

3 0
3 years ago
Other questions:
  • When purchasing a(n) ________ computer, having cellular as well as wifi can be important?
    9·1 answer
  • In what ways us cyberspace is real? list at least 3 examples to support your response .
    9·1 answer
  • What effect can camera tilt create? Do you think this is effective? Why or why not?
    13·1 answer
  • When was unicode invented?
    13·1 answer
  • Monica is teaching herself to paint. What web resource would be the most helpful
    14·1 answer
  • When discussing the data-modeling building blocks, anything (a person, a place, a thing, or an event) about which data are to be
    15·1 answer
  • a blank search examines the first item to see if it is a match, then the second, and so on. answers: linear, binary, and orderly
    7·2 answers
  • According to the video, which tasks do Police Patrol Officers perform? Select all that apply.
    15·2 answers
  • How would you open the web browser in Linux and still have access to the Linux terminal?
    14·1 answer
  • what extension of nat allows several hundred workstations to access the internet with a single public internet address
    10·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!