Answer.
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]
1 year 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]1 year 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
Osha focuses on personnel safety while process safety focuses on the prevention of __________
maw [93]

Answer/Explanation:

1. leaks in hazardous production processes; and

2. fire

4 0
1 year ago
How would a victim of phishing would most likely be contacted?
choli [55]

Answer: by email

Explanation:

5 0
1 year ago
Read 2 more answers
What are the seven internal compontents of a computer
likoan [24]
Motherboard, CPU, fans, Hard drives,Power Supply,Optical Drives and RAM
4 0
1 year ago
What is the difference between delete and backspace key?​
vladimir2022 [97]
They are the same thing
5 0
1 year ago
Read 2 more answers
Cars are only as safe as their driver, so __ is your bet to lower your risk.
lara [203]

seatbelt

Explanation:

Seatbelt lowers your momentum so it can take impact to be less dangerous

7 0
11 months ago
Other questions:
  • Which statement best describes an education at a vocational school?
    10·2 answers
  • When using a target drive that is fat32 formatted, what is the maximum size limitation for split files?â?
    12·1 answer
  • I have a midterm tomorrow for Comm Tech which is a class about using adobe photoshop. I can't download photoshop on my computer
    8·2 answers
  • While writing a program to regulate the speed of a self-driving car, you find that your software sometimes miscalculates the ide
    6·1 answer
  • Scientists are studying the effect of different temperatures on the processing speed of a computer chip. What is the independent
    5·2 answers
  • I need help please and thank you
    11·1 answer
  • Which of these is system software? Check all
    14·1 answer
  • Test if a number grade is an A (greater than or equal to 90). If so, print "Great!".
    13·2 answers
  • What are the parts of a file?​
    9·1 answer
  • Why is OpenAI recognized for changing the industry
    10·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!