LAB: Hailstone sequence (EO)

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

In Java please

6.20 LAB: Hailstone sequence (EO)

 

Given a positive integer num, the following rules will always create a sequence that ends with 1, called the hailstone sequence:

  • If num is even, divide it by 2
  • If num is odd, multiply it by 3 and add 1 (i.e. 3*num +1)
  • Continue until num is 1

Write a method, hailstone(), that takes in a positive integer as a parameter and prints the hailstone sequence starting with that integer. Format the output so that ten integers, each separated by a tab character (\t), are printed per line. Assume the input to the program is always positive.

The output format can be achieved as follows:
System.out.print(num + "\t");

Ex: If the program input is:

25

the output of method hailstone() is:

25 76 38 19 58 29 88 44 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1

Note: Your program must define the method:
public int hailstone(int num)

import java.util.Scanner;

public class Hailstone {

public static void hailstone(int num){
int i = 1;
while(num>0){

if(num%2==0){
System.out.print(num + "\t");
num = num / 2;
}
else {
System.out.print(num + "\t");
num = num * 3 + 1;
}
if(i==10){
i = 1;
System.out.print("");
}
else i++;
if(num==1){
System.out.println("1");
break;
}

}

}

public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
Hailstone labObject = new Hailstone();
int num;

num = scnr.nextInt();
labObject.hailstone(num); // Call hailstone() to print out the hailstone sequence.
}
}

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 4 steps with 2 images

Blurred answer
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education