Thursday, July 27, 2017

Hailstone Sequence

/* * This program generates hailstone sequence 
 * Use internet to find out how hailstone sequence works.
 */


public class HailstoneSequence {

    public static void sequenceCalculation(int num) {

        if(num == 0) {
            System.out.println("Please enter a natural number > 0:");
            return;
        }
        System.out.print(num);
        while(num!=1) {
            if(num % 2 == 0) {
                num = num/2;
            } else {
                num = 3*num + 1;
            }
            System.out.printf(", %d ", num);
        }
    }

    //main method    
public static void main(String [] args) {
        sequenceCalculation(7);
    }

}


   /*This program is not complete. It doesn't interact with the users.
  Use Scanner class to take user input and display the hailstone sequence.
 After doing some research you knew that the Hailstone sequence is a 
 sequence of numbers where the the nextnumber is division of 2 if the
 preceding num is even or if its odd then the following number will
 be 3 times the preceeding number plus 1.Mathematically:                 
                                      
                N(t+1) = N(t)/2 or 3N(t)+1; 
 
                       Where t = 1,2,3,4,...... ;             N = number;              
  */