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
Which of the following can you do under the fair use exceptions?
anzhelika [568]
What fair use exceptions are you talking about? There are thousands on fair use exceptions.
8 0
3 years ago
How do you find whether your system is 32 bit or 64 bit?
Shalnov [3]

Explanation:

If you are running windows on your system. Type system information in windows search box.

Go to my computer or this pc right click and select  properties and you can see there in system type the operating system and the process also.

it will be mentioned there the bits of the OS and system bits also in the same location.

8 0
3 years ago
Who plays rocket league?
Rashid [163]

Answer:

I dont

Explanation:

8 0
3 years ago
Read 2 more answers
Which component is the smallest unit in a spreadsheet?
nexus9112 [7]

Answer:

I believe it would be D:Cell :)

6 0
2 years ago
Read 2 more answers
Write a program that reads in non-negative integers and stores and displays distinct numbers (i.e., if a number appears multiple
slavikrds [6]

Answer:

See explaination

Explanation:

#include <iostream>

using namespace std;

#define MAX 1005

bool already_present(int data[MAX], int input, int size)

{

int i;

for(i=0;i<size;i++)

if(data[i] == input)

return true;

return false;

}

int read_stdin(int data[MAX])

{

int input;

int size=0;

while(true)

{

cout<<"Enter a non-negative integer (negative to quit): ";

cin>>input;

if(input<0)

break;

if(!already_present(data,input,size))

data[size++] = input;

}

return size;

}

void print_stdout(int data[MAX],int size)

{

int i;

cout<<"You entered\n";

for(i=0;i<size;i++)

cout<<data[i]<<" ";

cout<<endl;

}

int main()

{

int data[MAX],size;

size = read_stdin(data);

print_stdout(data,size);

return 1;

}

4 0
3 years ago
Other questions:
  • Assume the availability of a function is_prime. Assume a variable n has been associated with positive integer. Write the stateme
    7·1 answer
  • What are the 6 external parts of a computer system
    8·1 answer
  • PHOTOGRAPHY PLEASE HELP!
    15·1 answer
  • A lost update occurs when
    15·1 answer
  • 10. In step 4 of the CSMA/CA protocol, a station that successfully transmits a frame begins the CSMA/CA protocol for a second fr
    5·1 answer
  • How is the OR (||) logical operator used?<br> PLS HURRY
    11·1 answer
  • You can insert video by clicking video drop-down menu on the ______ tab.
    15·1 answer
  • When Mark completed his research paper, he decided that he wanted to have all headings to be in bold, underlined, and 14 points.
    11·1 answer
  • Which of the following is not related to text formatting?​
    11·1 answer
  • What type of software repairs or improves a larger application that is already installed on a system?
    9·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!