Posted on Leave a comment

Hailstone solution -Assignment 2

[code lang=”java”]/*
* File:
* Name:
* Section Leader:
* ——————–
* This file is the starter file for the Hailstone problem.

import acm.program.*;

public class Hailstone extends ConsoleProgram {
public void run() {
//index for the number of loops to do
int index =0;

//query for the number
int number = readInt("Enter a number: ");

//loop until the number ==1
//increment the loop

//if number is even take half
println(number+" is even so I take half: "+(number/=2));
} else {
//else if odd make it 3n+1
println(number+" is odd, so I make 3n +1: "+(number = (number*3)+1));
//print the number of loops
println("The process took "+index+" to reach 1.");


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.