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
jeka94
3 years ago
15

Write a function "hailstone"that takes an int "n2"and prints the hailstone sequence. Hailstone Numbers:This sequence takes the n

ame hailstone as the numbers generated bounce up and down. To generate the sequence, follow these rules:a.If the number isodd multiply it by 3 and add 1 b.If the number is even, divide by two.c.Repeat until you reach number 1.Sample Inputs & Outputsn2= 3-> 10, 5, 16, 8, 4, 2, 1 n2 =6-> 3, 10, 5, 16, 8, 4, 2, 1 n2 =7-> 22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1

Computers and Technology
1 answer:
givi [52]3 years ago
4 0

Answer:

Here is the C program    

#include<iostream>  //for input output functions

using namespace std;   //to identify objects like cin cout

int Hailstone(int n2);   //function declaration of function Hailstone

int main()  {  // start of main() function body

int num;   // stores number entered by user

cout<<"Enter a number: ";  // prompts user to enter a number

cin>>num;   // reads the input number

while(num!=1) {  // loop continues until the value of num becomes 1

 num = Hailstone(num);  // calls hailstone method and passes num to it

cout<<num<<"\n";  }   }  // prints the hailstone sequence

int Hailstone(int n2)  {  //function takes int n2 and prints hailstone sequence

if(n2 % 2 == 0) {  // if the number is even

return n2 /=2;  }  // divide the n2 by 2 if n2 value is even

else {  // if n2 is odd

return n2 = (3 * n2) + 1;  }  }  // multiply n2 by 3 and  add 1 if n2 is odd

 

Explanation:

The function Hailstone() takes as parameter an integer which is int type and is named is n2. This function prints the hailstone sequence. The function has an if statement that checks if the number n2 input by the user is even or odd. If the value of n2 is even which is found out by taking modulus of n2 by 2 and if the result is 0 this means that n2 is completely divisible by 2 and n2 is an even number. So if this condition evaluates to true then n2 is divided by 2. If the condition evaluates to false and the value of n2 is odd then n2 is multiplied by 3 and 1 is added.

In the main() function the user is prompted to enter a number and the  Hailstone() function is called which keeps repeating until the value of n2 reaches 1.

You might be interested in
Given an int variable n that has already been declared and initialized to a positive value , and another int variable j that has
Ugo [173]
While( n )  /*when n == 0, will fail*/
{
   putchar( '*' );  /*effectively print asterick*/
   n--;    /*post-decrement n*/
}
8 0
3 years ago
Question 1
Ivanshal [37]

Answer:

Q1. B

Q2. D

Explanation:

3 0
2 years ago
Read 2 more answers
To use   ( 2 complement ) answer it (101101)2 – (1100)2 =   (                    )2
Tju [1.3M]

Answer:

2(101101) - 2(1100) = 200002

Explanation:

the text is everywhere don't know what to answer

5 0
3 years ago
HELPPPPPPP!!!!!!!!!
Andrei [34K]
The compression ratio is 20:1 please mark me brainliest
3 0
3 years ago
Sort Sorts by value; assign new numbers as the keys.
JulijaS [17]

Answer: (a) sort()

Explanation:

Using sort() the keys are not preserved. However it sorts the array.

example : sort($array);

6 0
4 years ago
Other questions:
  • Legal counsel has notified the information security manager of a legal matter that will require the preservation of electronic r
    8·1 answer
  • websites in which a question appears at the top with boxes underneath that help answer the question are called?
    10·1 answer
  • A) A cable that is mainly used in the cable television network
    11·1 answer
  • From what location are the 1st computer instructions available on boot up?
    10·2 answers
  • List the memory units inside and outside of the CPU, along with short descriptions of their
    11·1 answer
  • In a paragraph of no less than 125 words, explain what netiquette is and how it improves efficiency and productivity in the work
    14·1 answer
  • If you decide you want to meet someone you met online, what should you do first? A. Tell your best friend. B. Call the person yo
    12·1 answer
  • Viruses and worms can affect a system by:
    11·2 answers
  • Type the correct answer in the box. Spell all words correctly.
    7·2 answers
  • In your opinion., which Zelda character is the hottest? Be honest.
    7·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!