DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Orchestration Meets MCP: Building Governed Agentic Workflows With Quarkus Flow and AGENTS.md
  • I Built a Java Version Manager by Fixing Other Tools' Open Bugs
  • Arrays in Java
  • The Java Story: The Official Documentary Is Here

Trending

  • Structured Logging in Distributed Systems: What Most Teams Get Wrong and How to Fix It
  • Agentic AI in 2026: How Autonomous AI Agents Are Replacing Manual Dev Work
  • A Practical Pipeline for Identifying Sensitive Columns Before Test Data Masking
  • Containerizing and Testing a Python Backtesting System With Docker and GitHub Actions
  1. DZone
  2. Coding
  3. Java
  4. How to generate Hailstone Sequence in Java?

How to generate Hailstone Sequence in Java?

By 
Jagadeesh Motamarri user avatar
Jagadeesh Motamarri
·
Sep. 20, 13 · Interview
Likes (16)
Comment
Save
Tweet
Share
31.7K Views

Join the DZone community and get the full member experience.

Join For Free

Program Statement

How to generate Hailstone Sequence in Java?

Solution

The Hailstone sequence of numbers can be generated from a starting positive integer, n by:

  • If n is 1 then the sequence ends.
  • If n is even then the next n of the sequence = n/2
  • If n is odd then the next n of the sequence = (3 * n) + 1

Code

?
package com.skilledmonster.examples.operations;
import java.util.Scanner;
/**
* Program to generate Hailstone Sequence.
* This program reads a number from the user then displays the Hailstone sequence
* for that number followed by a line that shows the number of steps taken to reach 1.
*
* @author Jagadeesh Motamarri
* @version 1.0
*/
public class HailstoneSequenceGenerator {
public static void main(String[] args) {
Scanner inputScanner = new Scanner(System.in);
System.out.printf("Enter a Number:  ");
try {
int number = inputScanner.nextInt();
int steps = 0;
while (number != 1) {
if (number % 2 == 0) {
System.out.println(number + " is even, so I take half: " + number / 2);
number /= 2;
} else {
System.out.println(number + " is odd, so I make 3n + 1: " + (number * 3 + 1));
number = number * 3 + 1;
}
steps++;
}
System.out.println("The process took " + steps + (steps < 2 ? " step" : " steps") + " to reach 1");
} catch (Exception e) {
System.out.println("Not a Number!! Run your Program again <img src="http://www.skilledmonster.com/wp-includes/images/smilies/icon_smile.gif" alt=":-)" class="wp-smiley"> ");
}
}
}

Output

hailstone_sequence   

As shown in the console output, for interger 17, it took 12 steps to reach 1 using Hailstone Sequence.

References

http://en.wikipedia.org/wiki/Collatz_conjecture


Java (programming language)

Published at DZone with permission of Jagadeesh Motamarri. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Orchestration Meets MCP: Building Governed Agentic Workflows With Quarkus Flow and AGENTS.md
  • I Built a Java Version Manager by Fixing Other Tools' Open Bugs
  • Arrays in Java
  • The Java Story: The Official Documentary Is Here

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook