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]
3 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]3 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
Give three facts about the history of the train shongololo
monitta
<span>began operating in 1995
shongololo means </span><span> “to roll up”
non- profit organization </span>
3 0
3 years ago
Not all output display data for humans to read explain why​
Vadim26 [7]

Answer:

The eye has a very focused view that is optimised for perceiving movement.Human cannot see clearly outside an -5 degree cone of foveal vision.

8 0
3 years ago
The "c" key and the "e" key are struck by
adoni [48]
1 because when you type on the home row your left middle finger will hit both of those letters.
3 0
3 years ago
Read 2 more answers
Why might you receive a tax refund from the irs
Harrizon [31]
Im not quite sure but i got mine back yesterday soooo (pretty good)

8 0
4 years ago
A person clicks on an ad of a fitness club in a blog on nutrition. The person conducts a search on yoga and moves to another web
ra1l [238]

Answer:

Option A. Third-party cookies

Explanation:

When you are browsing a website, this can store small pieces of useful data, based on your activity during your visit. This information is being collected by the website you are looking at, and you can see its domain on your address bar. Unlike regular cookies (or First-party cookies).

A third-party cookie belongs to a different domain than the one on your address bar. This is widely used in internet ads to show a person ads that will most likely interest him/her, according to where he/she is clicking. If you click on an internet ad, you will send information to the ad's domain, a cookie will be stored, stating that you are interested in this kind of ad. The advertiser can use these cookies to improve his advertising campaign or create more effective ads.

6 0
4 years ago
Other questions:
  • What form of internet access currently uses a technology called 4gb?
    10·1 answer
  • Which step is common to both creating a new document and saving a document?
    11·2 answers
  • Which kind of file would be hurt most by lossy compression algorithm
    8·2 answers
  • Can anyone help with this graded assignment for computer fundamentals
    10·1 answer
  • Which of the following operating systems includes a virtual assistant?<br> Group of answer choices
    8·1 answer
  • Which of these is NOT a reason for an IP Address?
    15·2 answers
  • Send me the answers<br>​
    15·1 answer
  • Who wanna be besties
    8·2 answers
  • The color in a circle is a _____________ data type.
    13·1 answer
  • Write this multiplacation statement as repeated addition 5/6 x 3​
    6·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!