Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

Hailstone sequence

// Java program to generate hailstone


// numbers and calculate steps required
// to reduce them to 1
import java.util.*;
class hailstone {
static int c;

// function to print hailstone numbers


// and to calculate the number of steps
// required
static int HailstoneNumbers(int N)
{
System.out.print(N + " ");

if (N == 1 && c == 0) {

// N is initially 1.
return c;
}
else if (N == 1 && c != 0) {

// N is reduced to 1.
c++;
return c;
}
else if (N % 2 == 0) {

// If N is Even.
c++;
HailstoneNumbers(N / 2);
}
else if (N % 2 != 0) {

// N is Odd.
c++;
HailstoneNumbers(3 * N + 1);
}
return c;
}

// Driver code
public static void main(String[] args)
{
int N = 7;
int x;

// Function to generate Hailstone


// Numbers
x = HailstoneNumbers(N);

// Output: Number of Steps


System.out.println();
System.out.println("Number of Steps: " + x);
}
}
Output:
7 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
Number of Steps: 17

You might also like