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
A credit given to an author for his or her work is called a(an)
Bad White [126]

Citation.

Any other related questions you want answered?

5 0
3 years ago
In a newspaper advertisement for a hybrid car, which of these presentation methods would be most effective
faltersainse [42]
Your answer is a....
6 0
3 years ago
Read 2 more answers
a cisc-style instruction set has a large number of high-level instructions that perform highly complex operations in a single st
NikAS [45]

Both desktop and laptop computers employ CISC (complex instruction set computer) CPUs. Small programs are on CISC machines. It requires a lot of time to execute because it contains a great deal of compound instructions.

<h3>What do you mean by CISC?</h3>

Both desktop and laptop computers employ CISC (complex instruction set computer) CPUs. More complex instructions can be processed by this kind of CPU. For instance, a single instruction might load two values, add them, and then store the outcome back in memory.

A computer that, in contrast to a computer with a limited instruction set, allows individual instructions to perform numerous processes and need a large number of cycles to complete (RISC).

Small programs are on CISC machines. It requires a lot of time to execute because it contains a great deal of compound instructions. In this case, a single set of instructions is safeguarded in stages; each instruction set contains more than 300 distinct instructions. On average, instructions take two to ten machine cycles to complete.

To learn more about CISC refer to:

brainly.com/question/13266932

#SPJ4

8 0
1 year ago
During which phase of the software development process are developers MOST likely to log bugs
sleet_krkn [62]

Answer:

The answer is the testing phase.

Explanation:

<em>Testing is the part of software development wherein developers performs test on the system to encounter bugs and possible errors before they would deploy the system. The aim of testing is to ensure that the system meets its objective (or the system perform its desired function). In any case that there is a bug or error present during the test, testers must logged it in and developers must go back to the coding stage to fix bugs they found in codes and system process.  Then testers would run series of tests again to ensure that the previous errors are gone. This will be repeatedly executed until the system is down to 0 errors before it will be introduced to the clients or desired users of the system.</em>

4 0
3 years ago
Read 2 more answers
Write a Python program to check whether an alphabet is a vowel or consonant.<br>​
Anton [14]

Answer:

The program to this question an be given as:

Program:

char = input("Enter any alphabet: ") #input alphabet from user in char variable.

if char in ('a', 'e', 'i', 'o', 'u'): #check condition in if block.

print("vowel.")  #print value.

else:   #else block

print("consonant.")  #print value.

Output:

Enter any alphabet: r

consonant.

Explanation:

In the above python program firstly we define a variable that is "char" it is used to take input from the user. for user input, we use the input function in the python.

Then we define the conditional statement in this statement in if block we pass alphabets that is 'a', 'e', 'i', 'o', 'u' and check if user input alphabet and this alphabet match it will print vowel. otherwise, it will print consonant.  

8 0
3 years ago
Other questions:
  • Which directory in the FHS stores programs and configuration information that can only be executed and modified by the root user
    10·1 answer
  • A real-world problem that might be explained or predicted through the creation of a computer simulation
    7·1 answer
  • i'm actually really smart so if u need help just ask me or leave me a comment. im 16 and my name is lexi
    8·2 answers
  • Given a String variable response that has already been declared, write some code that repeatedly reads a value from standard inp
    10·1 answer
  • Easy coding question, please help.
    13·1 answer
  • R6. Suppose N people want to communicate with each of N - 1 other peo- ple using symmetric key encryption. All communication bet
    13·1 answer
  • 4.8 Code Practice: Question 1
    11·2 answers
  • How do i take off securly from my chromebook
    12·2 answers
  • : Describe the type of gameplay seen during early video games (ex. Spacewar!, Pong).
    9·1 answer
  • You are creating a program that can add up the amount of money the user spent that day. Right down to the penny! What kind of va
    12·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!